| | |
using strstr to find words
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Solved Threads: 0
I did a quick search on the forums and there was another problem similar to the one i'm trying to solve.
I'm supposed to use the first word of a sentence as a search parameter and check the rest of the string for occurences of the word. If the word appears again, increment a counter.
The problem I'm having is that when I use the function strstr() in a while loop, the console hangs :rolleyes: .
Correct me if i'm wrong, but the strstr() function returns a NULL if the substring isn't found in the main string. So, if my sentence was "the cat sat on the mat" the loop would terminate after the second "the."
I'm supposed to use the first word of a sentence as a search parameter and check the rest of the string for occurences of the word. If the word appears again, increment a counter.
The problem I'm having is that when I use the function strstr() in a while loop, the console hangs :rolleyes: .
Correct me if i'm wrong, but the strstr() function returns a NULL if the substring isn't found in the main string. So, if my sentence was "the cat sat on the mat" the loop would terminate after the second "the."
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> #define SIZE 100 #define FLAG 1 void search_string(char *); char string[SIZE]; int main(void) { char array[SIZE]; puts("Enter a string:"); gets(array); search_string(array); return 0; } void search_string(char * array) { char *ptr, *strptr; int i = 0, j = 1; ptr = array; while (*ptr != '\0') { if (isspace(*ptr)) break; string[i] = array[i]; //put each char from array into string until a space is found. ptr++; i++; } strptr = strstr(array, string); /* while (FLAG == 1) { if (strptr != 0) j++; else FLAG == 0; } */ printf("The first word is: %s\n", string); printf("The sentence is: %s\n", array); printf("The first word occured %d times in the sentence\n", j); }
> The problem I'm having is that when I use the function strstr() in a while loop, the console hangs
Well you don't advance the pointer, so it always begins the search from the same point, always finds the same match, and so on on on on .....
> gets(array);
NEVER use gets() to read input, there is no way to make it safe.
Always use fgets().
> char string[SIZE];
This should really be a local variable in some function.
Whether it's in main, and passed as a parameter to your function, or as a local within your function is up to you.
> while (*ptr != '\0')
This loop doesn't seem to append the \0 to the string you create.
Well you don't advance the pointer, so it always begins the search from the same point, always finds the same match, and so on on on on .....
C Syntax (Toggle Plain Text)
while ( (p=strstr(p,"thing")) != NULL ) { // do something // start search from just past the previous hit p++; }
> gets(array);
NEVER use gets() to read input, there is no way to make it safe.
Always use fgets().
> char string[SIZE];
This should really be a local variable in some function.
Whether it's in main, and passed as a parameter to your function, or as a local within your function is up to you.
> while (*ptr != '\0')
This loop doesn't seem to append the \0 to the string you create.
•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Solved Threads: 0
:cry: :cry: :cry: :cry: :cry:
Ok i finally solved it. Thanks for the help
Ok i finally solved it. Thanks for the help
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #include <ctype.h> #define SIZE 100 void search_string(char *); int main(void) { char array[SIZE]; puts("Enter a string:"); gets(array); search_string(array); return 0; } void search_string(char *array) { char *ptr, *strptr, word[SIZE] = {""}; int i = 0, j = 0; ptr = array; while (*ptr != '\0') { if (isspace(*ptr)) break; word[i] = array[i]; ptr++; i++; } strptr = array; while ((strptr = strstr(strptr, word)) != NULL) { strptr++; j++; } puts(array); printf("%s occured %d times in the string\n", word, j); }
•
•
Join Date: Dec 2007
Posts: 1
Reputation:
Solved Threads: 0
to degamer 106:
hey there, looking at your source code was a big help to me but i think it might be better to step 'strptr' through the entire length of 'word', like this:
strptr=strptr+strlen(word);
i think this could be faster, but please correct me if i have made a wrong judgement, waiting to here from you,
cheers!
hey there, looking at your source code was a big help to me but i think it might be better to step 'strptr' through the entire length of 'word', like this:
strptr=strptr+strlen(word);
i think this could be faster, but please correct me if i have made a wrong judgement, waiting to here from you,
cheers!
![]() |
Similar Threads
- Dirty words filter... (DaniWeb Community Feedback)
- Scan text file to find all words of 4 characters or less (Shell Scripting)
- knoppix or else (Getting Started and Choosing a Distro)
- meta tags? (Search Engine Optimization)
Other Threads in the C Forum
- Previous Thread: hash- linear probing
- Next Thread: program that will input a number and display it in words
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks bash binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql number open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming stack standard strchr string strings structures suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






