| | |
Problem in String Search:Please Help
![]() |
•
•
•
•
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,
C Syntax (Toggle Plain Text)
/*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 !!
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 !!
•
•
•
•
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!
Hope it helped, bye.
I don't accept change; I don't deserve to live.
•
•
•
•
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!!!
C Syntax (Toggle Plain Text)
#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; }
Last edited by s s paul; Aug 22nd, 2006 at 11:21 pm.
First, this indicates that you seem to be compiling as C++ (or C99) instead of C(89).
Staying in C or staying in C++ is preferable to mixing while you're starting out.
You use And then you drop the ball and mix it with And improperly.
User Input: Strings and Numbers [C]
Now
I think you've got the print and the fix-it backwards here.
I haven't looked much more at the code, but it's starting to sound like
Strings: Finding a Substring
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;You use
fgets from the start. C Syntax (Toggle Plain Text)
if ( fgets(buf, sizeof(buf), stdin) != NULL )
scanf. scanf("%s",&word);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.
C Syntax (Toggle Plain Text)
printf ("Thank you, you entered >%s<\n", buf); /* * Now test for, and remove that newline character */ if ( (c= strchr(buf, '\n')) != NULL ) *c = '\0';
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
![]() |
Similar Threads
- Problem with string variable in void prnt function (C++)
- Problem with string search of an array (C++)
Other Threads in the C Forum
- Previous Thread: Modifying Form's Clientsize in a static function
- Next Thread: help me pls!!!some of my functions doesn't work!
| Thread Tools | Search this Thread |
#include * adobe ansi array asterisks binarysearch centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux list locate looping lowest match matrix meter microsoft number oddnumber opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






