Friends I am making a program to seach a string from a input line of strings.There are two problems :
(i)The program is giving correct search for word that is the 1st word of input line. e.g.,"hello" in hello friends how are you? but unsuccessful search for any word that is not the 1st word like "friends" or "how" in the above mentioned sentence.
(ii)I cannot input multiple lines starting from newline with gets or scanf as they stop reading when encounter new-line character.The input termination may be indicated by another special character like # instead of new-line.
The program I hope is self-explanatory .The logic is to use a pointer to word(*p) & input line buffer(*a) & match the character by character of the word in line input if not match the line input pointer(*a) is incremented till it reaches the 1st letter of next word,

/*program to take a line of input from user in 
char buf[BUFSIZ] and to search for the 
 word entered in char word[5] in the previous input line*/ 
 
/*Run on Linux platform gives error that if the word to be 
searched is successfully searched only if it is the 
1st word of input line */
 
#include<stdio.h>
#include<string.h>
 
int main()
{
char  word[5];i
char *a,*p,*b;
b=p=word;
//flag checks if a match has been found
int flag=0,len,count=0,disp=0;
char buf[BUFSIZ];
char *c;
a=buf;
 printf ("Please enter a line of text, max %d characters\n", sizeof(buf));
 
 if (fgets(buf, sizeof(buf), stdin) != NULL)
 {
   printf ("Thank you, you entered >%s<\n", buf);
 
   /*
    *  Now test for, and remove that newline character
    */
   if ((c= strchr(buf, '\n')) != NULL)
     *c = '\0';
}
 
printf("\n\nEnter word needed\n");
printf("\n\nYou entered %s of length %d",word,len);
while(*a!='\0')
 {
   if(*a == *p)
      {/*count shows how many continuous match of letters takes place*/
       a++;p++;count++;printf("\nCount=%d",count);
      }
   else
      {
         if((count==len || count==len-1)
           &&(*a ==' '|| *a == '\t' || *a == '\0'))
             {
                 printf("\n\nMAtch");flag=1;
                  break;
             }
         else
            {  if(*a == '\0')
                   { break ;}
                else
                    {
                     count=0;
                     while(*a != ' '|| *a != '\t' &&  *a != '\0' )
                          { if(*a=='\0')
                                     break;
                              else
                                  {
 /*disp is used to know how many times this loop works.*/
/*The Disp values printed shows that pointer(a)
 is incremented to end  while 
no checking for matching is done*/
                                     a++;disp++;
                                 printf("\nDisp=%d",disp);
                                  }
                           }
                    if(*a != '\0')
                       { a++;p=b;}
                     }
            }
      }
  if((*a == '\0') &&(count==len || count==len-1))
         {  printf("\n\nMatch");
            flag=1;
          }
 }
if(flag==0)
printf("\n\n\nNo Match");
return 0; --                                              
}

Hi Friends!
Sorry it was my 1st post so problem in copying code down .The four lines before 1st while loop (while(*a !='\0'))are:

printf("\n\nEnter word needed\n");
scanf("%s",word);
len=strlen(word);/*len stores string length of word*/
printf("\n\nYou entered %s of length %d",word,len);

of which a partial part has been transfered in my post.
Also there is no use of char i.
Do forgive me !!

It would be better if u post the entire code without just propagating the updates like an AJAX enabled page :)

Since word is defined as 5 characters, and you enter "hello", the \0 at the end overwrites memory outside your string. Try increasing the size of word for starters.

Since word is defined as 5 characters, and you enter "hello", the \0 at the end overwrites memory outside your string. Try increasing the size of word for starters.

Hi WaltP,
I did increase the size of char array word to 10 still the problems persists!!

Read the post from S.O.S.

won't u forgive me for 1st mistake in my 1st post!!
If u don't have any technical sugestion about the problem over which I have shown some effort at least then plz don't send them.
This may discourage others!

won't u forgive me for 1st mistake in my 1st post!!
If u don't have any technical sugestion about the problem over which I have shown some effort at least then plz don't send them.
This may discourage others!

Buddy we not discouraging anyone, we just need the code i mean the complete code which you have written to understand the problem at hand and help you out. And by the way we no discouraging anyone, just asking a little dedication from your side so you can one day program with the basics on your fingertips.

Hope it helped, bye.

So as requested I am sending the complete code & hope u'll help to find the bug out!!The logic is to use a pointer "a" point to each word of buff & "p" points to the word to be searched.If on comparison contents of pointer "a" & "p" match both are incremented else "a" is incremented till it encounters space or tab & pointer "p" is initialized back to point to 1st letter of word.
Problem:only if the word is the 1st one of input line,search is successful else not.

Hope everything else will be clear thro the comments inside the program!!!

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
 
char word[10];
char *a,*p,*b;
b=p=word;
//b is storing the base address for future reference
 int flag=0,len,count=0,disp=0;
char buf[BUFSIZ];
char *c;
a=buf;
if (fgets(buf, sizeof(buf), stdin) != NULL)
{
printf ("Thank you, you entered >%s<\n", buf);
/*
* Now test for, and remove that newline character
*/
if ((c= strchr(buf, '\n')) != NULL)
*c = '\0';
}
printf("\n\nEnter word needed\n");
scanf("%s",&word);
len=strlen(word);//len has the length of word to be searched
printf("\n\nYou entered %s of length %d",word,len);
while(*a!='\0')
{
if(*a == *p)
{//count takes the number of continuous matches
a++;p++;count++;printf("\nCount=%d",count);
}
else
{ /*"count == len-1" is used when word to be searched is last one in input line as then '\0' wont be matched*/
       if((count==len || count==len-1)
       &&(*a ==' '|| *a == '\t' || *a == '\0'))
        {
         printf("\n\nMAtch");flag=1;
         break;
         }
     else
        { if(*a == '\0')
            { break ;}
          else
           {
            count=0;
               while(*a != ' '|| *a != '\t' && *a != '\0' )
                 { if(*a=='\0')
                   break;
                    else
                      {/*disp is used to see how the loop works &  
                        suppose here is some problem*/
                       a++;disp++;
                       printf("\nDisp=%d,Value pointed by a:%c",
                               disp,*a);
                       }
                     if(*a != '\0')
                       { a++;p=b;}
                     if(*a == *p)
                         {
                         a++;p++;count++;printf("\nCount=%d",count);
                           }
                    else
                       {
                        if((count==len || count==len-1)
                         &&(*a ==' '|| *a == '\t' || *a == '\0'))
                            {
                              printf("\n\nMAtch");flag=1;
                                break;
                            }
                        }
                      }
                  }
         }
}
      if((*a == '\0') &&(count==len || count==len-1))
      { printf("\n\nMatch");
         flag=1;
       }
}
if(flag==0)
printf("\n\n\nNo Match");
return 0;
}

First, this indicates that you seem to be compiling as C++ (or C99) instead of C(89).

int main()
{
 
char word[10];
char *a,*p,*b;
b=p=word;
//b is storing the base address for future reference
 int flag=0,len,count=0,disp=0;
char buf[BUFSIZ];
char *c;
a=buf;

Staying in C or staying in C++ is preferable to mixing while you're starting out.

You use fgets from the start.

if ( fgets(buf, sizeof(buf), stdin) != NULL )

And then you drop the ball and mix it with scanf .

scanf("%s",&word);

And improperly.
User Input: Strings and Numbers [C]

Now scanf is an unruly beast, so if you are well versed in what it is you are telling it, just avoid it.

I think you've got the print and the fix-it backwards here.

printf ("Thank you, you entered >%s<\n", buf);
      /*
       * Now test for, and remove that newline character
       */
      if ( (c= strchr(buf, '\n')) != NULL )
         *c = '\0';

I haven't looked much more at the code, but it's starting to sound like strstr .
Strings: Finding a Substring

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.