Need help with string loop

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2004
Posts: 11
Reputation: ellas747 is an unknown quantity at this point 
Solved Threads: 0
ellas747 ellas747 is offline Offline
Newbie Poster

Need help with string loop

 
0
  #1
Nov 18th, 2004
I have this program but a liitle confused about getting the loop to work. i have to write a program that inputs a text and a search string from the keyboard. using a function strstr locate the first occurrence of the search string of the line of text, and assign the location to a variable searchptr of type char* if the search string is found print the remaninder of the line of the text beginning with the search string then use strstr again to locate the next occurrence of the search string in the line of text. if a second occurrence is found print the remainder of the line beiginning with the second occurrence. hint: the second call to strstr should contain searchptr plus 1 as its first argument.

[code] so far i have this, can somebody help me. Thanks
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int main()
{
char line1[80];
char line2[80];
char *searchptr;
int i;

char found[80];

printf( "Enter Sentence\n");
gets(line1);

printf(" Enter String to Search: ");
gets(line2);

searchptr=strstr(line1, line2);
printf ("%s\n",searchptr);
while (searchptr!=NULL)
{
searchptr=strstr(line1, line2)+1;

}

return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Need help with string loop

 
0
  #2
Nov 18th, 2004
inside the loop try:

searchptr=strstr(searchptr+1, line2);

You want to start each subsequent search at one char past where the last search stopped.

Oh, yeah, and move the printf INSIDE the loop so a) you print each string found, and
b) you DON'T print when searchptr is NULL:
  1. searchptr=strstr(line1, line2);
  2. while (searchptr!=NULL)
  3. {
  4. printf ("%s\n",searchptr);
  5. searchptr=strstr(searchptr+1, line2);
  6. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 11
Reputation: ellas747 is an unknown quantity at this point 
Solved Threads: 0
ellas747 ellas747 is offline Offline
Newbie Poster

Re: Need help with string loop

 
-1
  #3
Nov 30th, 2004
I tried doing this program but its still not working. Can somebody help me out.
Thanks,

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int main()
{
char line1[80];
char line2[80];
char *searchptr;
int i;

char found[80];

printf( "Enter Sentence\n");
gets(line1);

printf(" Enter String to Search: ");
gets(line2);

searchptr=strstr(line1, line2);
while (searchptr!=NULL)
{
searchptr=strstr(searchptr+1, line2);
printf ("%s\n",searchptr);

}

return 0;
}
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC