| | |
Need help with string loop
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2004
Posts: 11
Reputation:
Solved Threads: 0
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;
}
[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;
}
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:
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:
C Syntax (Toggle Plain Text)
searchptr=strstr(line1, line2); while (searchptr!=NULL) { printf ("%s\n",searchptr); searchptr=strstr(searchptr+1, line2); }
•
•
Join Date: Sep 2004
Posts: 11
Reputation:
Solved Threads: 0
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;
}
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;
}
![]() |
Similar Threads
- How does one tolower an entire string? (C++)
- Scanning a string? (Python)
- Replacing items in a string using a loop? (Python)
- Connection String (Visual Basic 4 / 5 / 6)
- string array size (C++)
- Creating a Basic String Database (C++)
- string parser (Shell Scripting)
Other Threads in the C Forum
- Previous Thread: Ive Been Reading
- Next Thread: Decimal to Base 2 - 16 Conversion
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming stack standard string strings structures systemcall testautomation turboc unix user variable voidmain() wab win32api windows.h





