944,030 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1904
  • C RSS
Aug 19th, 2006
0

Problem in String Search:Please Help

Expand Post »
Quote ...
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,
  1.  
  2. /*program to take a line of input from user in
  3. char buf[BUFSIZ] and to search for the
  4.  word entered in char word[5] in the previous input line*/
  5.  
  6. /*Run on Linux platform gives error that if the word to be
  7. searched is successfully searched only if it is the
  8. 1st word of input line */
  9.  
  10. #include<stdio.h>
  11. #include<string.h>
  12.  
  13. int main()
  14. {
  15. char word[5];i
  16. char *a,*p,*b;
  17. b=p=word;
  18. //flag checks if a match has been found
  19. int flag=0,len,count=0,disp=0;
  20. char buf[BUFSIZ];
  21. char *c;
  22. a=buf;
  23. printf ("Please enter a line of text, max %d characters\n", sizeof(buf));
  24.  
  25. if (fgets(buf, sizeof(buf), stdin) != NULL)
  26. {
  27. printf ("Thank you, you entered >%s<\n", buf);
  28.  
  29. /*
  30.   * Now test for, and remove that newline character
  31.   */
  32. if ((c= strchr(buf, '\n')) != NULL)
  33. *c = '\0';
  34. }
  35.  
  36. printf("\n\nEnter word needed\n");
  37. printf("\n\nYou entered %s of length %d",word,len);
  38. while(*a!='\0')
  39. {
  40. if(*a == *p)
  41. {/*count shows how many continuous match of letters takes place*/
  42. a++;p++;count++;printf("\nCount=%d",count);
  43. }
  44. else
  45. {
  46. if((count==len || count==len-1)
  47. &&(*a ==' '|| *a == '\t' || *a == '\0'))
  48. {
  49. printf("\n\nMAtch");flag=1;
  50. break;
  51. }
  52. else
  53. { if(*a == '\0')
  54. { break ;}
  55. else
  56. {
  57. count=0;
  58. while(*a != ' '|| *a != '\t' && *a != '\0' )
  59. { if(*a=='\0')
  60. break;
  61. else
  62. {
  63. /*disp is used to know how many times this loop works.*/
  64. /*The Disp values printed shows that pointer(a)
  65.  is incremented to end while
  66. no checking for matching is done*/
  67. a++;disp++;
  68. printf("\nDisp=%d",disp);
  69. }
  70. }
  71. if(*a != '\0')
  72. { a++;p=b;}
  73. }
  74. }
  75. }
  76. if((*a == '\0') &&(count==len || count==len-1))
  77. { printf("\n\nMatch");
  78. flag=1;
  79. }
  80. }
  81. if(flag==0)
  82. printf("\n\n\nNo Match");
  83. return 0; --
  84. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
s s paul is offline Offline
10 posts
since Aug 2006
Aug 19th, 2006
0

Re: Problem in String Search:Please Help

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 !!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
s s paul is offline Offline
10 posts
since Aug 2006
Aug 19th, 2006
0

Re: Problem in String Search:Please Help

It would be better if u post the entire code without just propagating the updates like an AJAX enabled page
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Aug 20th, 2006
1

Re: Problem in String Search:Please Help

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.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Aug 20th, 2006
0

Re: Problem in String Search:Please Help

Quote originally posted by WaltP ...
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!!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
s s paul is offline Offline
10 posts
since Aug 2006
Aug 20th, 2006
0

Re: Problem in String Search:Please Help

Read the post from S.O.S.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Aug 21st, 2006
0

Re: Problem in String Search:Please Help

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!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
s s paul is offline Offline
10 posts
since Aug 2006
Aug 22nd, 2006
0

Re: Problem in String Search:Please Help

Click to Expand / Collapse  Quote originally posted by s s paul ...
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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Aug 22nd, 2006
0

Re: Problem in String Search:Please Help

Quote ...
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!!!
  1.  
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. int main()
  6. {
  7.  
  8. char word[10];
  9. char *a,*p,*b;
  10. b=p=word;
  11. //b is storing the base address for future reference
  12. int flag=0,len,count=0,disp=0;
  13. char buf[BUFSIZ];
  14. char *c;
  15. a=buf;
  16. if (fgets(buf, sizeof(buf), stdin) != NULL)
  17. {
  18. printf ("Thank you, you entered >%s<\n", buf);
  19. /*
  20. * Now test for, and remove that newline character
  21. */
  22. if ((c= strchr(buf, '\n')) != NULL)
  23. *c = '\0';
  24. }
  25. printf("\n\nEnter word needed\n");
  26. scanf("%s",&word);
  27. len=strlen(word);//len has the length of word to be searched
  28. printf("\n\nYou entered %s of length %d",word,len);
  29. while(*a!='\0')
  30. {
  31. if(*a == *p)
  32. {//count takes the number of continuous matches
  33. a++;p++;count++;printf("\nCount=%d",count);
  34. }
  35. else
  36. { /*"count == len-1" is used when word to be searched is last one in input line as then '\0' wont be matched*/
  37. if((count==len || count==len-1)
  38. &&(*a ==' '|| *a == '\t' || *a == '\0'))
  39. {
  40. printf("\n\nMAtch");flag=1;
  41. break;
  42. }
  43. else
  44. { if(*a == '\0')
  45. { break ;}
  46. else
  47. {
  48. count=0;
  49. while(*a != ' '|| *a != '\t' && *a != '\0' )
  50. { if(*a=='\0')
  51. break;
  52. else
  53. {/*disp is used to see how the loop works &
  54.   suppose here is some problem*/
  55. a++;disp++;
  56. printf("\nDisp=%d,Value pointed by a:%c",
  57. disp,*a);
  58. }
  59. if(*a != '\0')
  60. { a++;p=b;}
  61. if(*a == *p)
  62. {
  63. a++;p++;count++;printf("\nCount=%d",count);
  64. }
  65. else
  66. {
  67. if((count==len || count==len-1)
  68. &&(*a ==' '|| *a == '\t' || *a == '\0'))
  69. {
  70. printf("\n\nMAtch");flag=1;
  71. break;
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
  78. if((*a == '\0') &&(count==len || count==len-1))
  79. { printf("\n\nMatch");
  80. flag=1;
  81. }
  82. }
  83. if(flag==0)
  84. printf("\n\n\nNo Match");
  85. return 0;
  86. }
Last edited by s s paul; Aug 22nd, 2006 at 11:21 pm.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
s s paul is offline Offline
10 posts
since Aug 2006
Aug 22nd, 2006
0

Re: Problem in String Search:Please Help

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.
  1. 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.
  1. printf ("Thank you, you entered >%s<\n", buf);
  2. /*
  3.   * Now test for, and remove that newline character
  4.   */
  5. if ( (c= strchr(buf, '\n')) != NULL )
  6. *c = '\0';
I haven't looked much more at the code, but it's starting to sound like strstr.
Strings: Finding a Substring
Last edited by Dave Sinkula; Aug 22nd, 2006 at 11:48 pm.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Modifying Form's Clientsize in a static function
Next Thread in C Forum Timeline: help me pls!!!some of my functions doesn't work!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC