Saturday, September 3, 2011

String Operations


// Create linked list of strings.
// Display all strings containing same substring
//(Accept substring from user)

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct node
{
char str[100];
struct node *next,*prev;
}node;
int main()
{
char substr[20];
int present=0;
node *head,*h,*p;
head=NULL;
h=p=NULL;
printf("\nEnter the Strings to be stored in Linked List (Enter Exit to stop)");
head=(node *)malloc(sizeof(node));
gets(head->str);
        fflush(stdin);
if(strcasecmp(head->str,"exit")==0)
{
printf("\n\n\tEMPTY LIST ");
exit(0);
}
head->next=NULL;
head->prev=NULL;
h=head;

while(1)
{
p=(node *)malloc(sizeof(node));
gets(p->str);
fflush(stdin);
if(strcasecmp(p->str,"exit")==0)
{
free(p);
break;
}
h->next=p;
p->prev=h;
p->next=NULL;
h=p;
}


printf("\nEnter the substring:");
gets(substr);
fflush(stdin);

printf("\nAll the Strings containing the Gives Substring are as Follows:");

h=head;
while(h!=NULL)
{
if(strstr(h->str,substr)!=NULL)
{
present=1;
printf("\n%s",h->str);
}
h=h->next;
}

if(present==0)
printf("\n There is no string which has said substring.");


}

No comments:

Post a Comment