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,
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!!
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.
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!!!
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.
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.