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."

#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 .....

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;
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.

:cry: :cry: :cry: :cry: :cry:

Ok i finally solved it. Thanks for the help

#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);
}

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!

This article has been dead for over six months. Start a new discussion instead.