Problem in String Search:Please Help

Reply

Join Date: Aug 2006
Posts: 10
Reputation: s s paul is an unknown quantity at this point 
Solved Threads: 1
s s paul's Avatar
s s paul s s paul is offline Offline
Newbie Poster

Problem in String Search:Please Help

 
0
  #1
Aug 19th, 2006
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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 10
Reputation: s s paul is an unknown quantity at this point 
Solved Threads: 1
s s paul's Avatar
s s paul s s paul is offline Offline
Newbie Poster

Re: Problem in String Search:Please Help

 
0
  #2
Aug 19th, 2006
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 !!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Problem in String Search:Please Help

 
0
  #3
Aug 19th, 2006
It would be better if u post the entire code without just propagating the updates like an AJAX enabled page
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Problem in String Search:Please Help

 
1
  #4
Aug 20th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 10
Reputation: s s paul is an unknown quantity at this point 
Solved Threads: 1
s s paul's Avatar
s s paul s s paul is offline Offline
Newbie Poster

Re: Problem in String Search:Please Help

 
0
  #5
Aug 20th, 2006
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!!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Problem in String Search:Please Help

 
0
  #6
Aug 20th, 2006
Read the post from S.O.S.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 10
Reputation: s s paul is an unknown quantity at this point 
Solved Threads: 1
s s paul's Avatar
s s paul s s paul is offline Offline
Newbie Poster

Re: Problem in String Search:Please Help

 
0
  #7
Aug 21st, 2006
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!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Problem in String Search:Please Help

 
0
  #8
Aug 22nd, 2006
Originally Posted by s s paul View Post
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 10
Reputation: s s paul is an unknown quantity at this point 
Solved Threads: 1
s s paul's Avatar
s s paul s s paul is offline Offline
Newbie Poster

Re: Problem in String Search:Please Help

 
0
  #9
Aug 22nd, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Problem in String Search:Please Help

 
0
  #10
Aug 22nd, 2006
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC