when I enter a sentence, this code should get the first word and count the how many times repeated. firstWord(): This function gets the string as a parameter. Then, outputs the total number of times the first word occurs. (this code consisting of 4 functions and in this func(firstWord()) I should use 'string' as a parameter, in main function I just call the firstWord func. ) number of times the first word occurs. for ex: sentence is no changes and no suprises first word no repeated 2 times. here is the code.
void firstWord(char st3[]){
char firstw[20];
char *ptr;
int i=0;
int count=0;
ptr=strstr(st3,firstw);
while (ptr = '\0')
{
count ;
ptr ;
ptr = strstr(ptr, firstw);
}
printf("First word %s is repeated %d times\n", firstw, count);
}
CodePudding user response:
The function does not make a sense.
You are using the uninitialized array firstw
in a call of strtsr
as for example
char firstw[20];
//..
ptr=strstr(st3,firstw);
that invokes undefined behavior.
Also there is a typo in the condition of the while statement
while (ptr = '\0')
Instead of using the equality operator ==
you are using the assignment operator =
after which the pointer ptr
becomes a null pointer.
At first you need to find the first word in the source string and then count how many times it is present in the source string.
Pay attention to that the function strstr can find a substring that does not form a word that is that is not delimited by white space characters.
Also at least the parameter of the function firstWord
should be declared with the qualifier const
because the source string is not changed within the function. And the return type of the function should not be void. The function should return the counter and the position and the length pf the first word in the source string as for example by using an object of a structure type.
The function can be implemented for example the following way as shown in the demonstration program below.
#include <stdio.h>
#include <string.h>
struct Word
{
char *pos;
size_t length;
size_t counter;
};
struct Word firstWord( const char *s )
{
const char *delim = " \t\n";
struct Word word = { .pos = NULL, .length = 0, .counter = 0 };
while ( *s )
{
s = strspn( s, delim );
if ( *s )
{
size_t n = strcspn( s, delim );
if ( word.pos == NULL )
{
word.pos = ( char * )s;
word.length = n;
word.counter = 1;
}
else
{
if ( n == word.length && strncmp( word.pos, s, n ) == 0 )
{
word.counter;
}
}
s = n;
}
}
return word;
}
int main( void )
{
const char *s = " A AB A ABC A";
struct Word word = firstWord( s );
if ( word.pos != NULL )
{
printf( "The first word \"%.*s\" is found %zu time(s)\n",
word.length, word.pos, word.counter );
}
}
The program output is
The first word "A" is found 3 time(s)
Using that function you can for example sort an array of strings in the lexicographical order of first words and if the words are equal in the ascending order of their counters in strings.
Here is a demonstration program.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Word
{
char *pos;
size_t length;
size_t counter;
};
struct Word firstWord( const char *s )
{
const char *delim = " \t\n";
struct Word word = { .pos = NULL, .length = 0, .counter = 0 };
while ( *s )
{
s = strspn( s, delim );
if ( *s )
{
size_t n = strcspn( s, delim );
if ( word.pos == NULL )
{
word.pos = ( char * )s;
word.length = n;
word.counter = 1;
}
else
{
if ( n == word.length && strncmp( word.pos, s, n ) == 0 )
{
word.counter;
}
}
s = n;
}
}
return word;
}
int cmp( const void *a, const void *b )
{
const char *s1 = *( const char * const * )a;
const char *s2 = *( const char * const * )b;
struct Word word1 = firstWord( s1 );
struct Word word2 = firstWord( s2 );
size_t n = word1.length;
if ( word2.length < n ) n = word2.length;
int result = strncmp( word1.pos, word2.pos, n );
if ( result == 0 )
{
result = ( word2.length < word1.length ) - ( word1.length < word2.length );
if ( result == 0 )
{
result = ( word2.counter < word1.counter ) - ( word1.counter < word2.counter );
}
}
return result;
}
int main( void )
{
const char * s[] =
{
"CCC CCC CCC",
"CCC CCC",
"BB BB",
"A A",
"CC",
"BB",
"CCC",
"A"
};
const size_t N = sizeof( s ) / sizeof( *s );
for ( size_t i = 0; i < N; i )
{
puts( s[i]);
}
putchar( '\n' );
qsort( s, N, sizeof( const char * ), cmp );
for ( size_t i = 0; i < N; i )
{
puts( s[i]);
}
putchar( '\n' );
}
The program output is
CCC CCC CCC
CCC CCC
BB BB
A A
CC
BB
CCC
A
A
A A
BB
BB BB
CC
CCC
CCC CCC
CCC CCC CCC
If you need to write a function that just determines the first word in a string then it can look the following way as shown in the demonstration program below.
#include <stdio.h>
#include <string.h>
struct Word
{
char *pos;
size_t length;
};
struct Word firstWord( const char *s, const char *delim )
{
struct Word word = { .pos = NULL, .length = 0 };
while ( *s && word.pos == NULL )
{
s = strspn( s, delim );
if ( *s )
{
size_t n = strcspn( s, delim );
word.pos = ( char * )s;
word.length = n;
s = n;
}
}
if ( word.pos == NULL )
{
word.pos = ( char * )s;
}
return word;
}
int main( void )
{
const char *s = " A AB BA A";
struct Word word = firstWord( s, " \t\n" );
char first_word[word.length 1];
strncpy( first_word, word.pos, word.length );
first_word[word.length] = '\0';
printf( "The first word is \"%s\" at position %zu\n",
first_word, word.pos - s );
}
The program output is
The first word is "A" at position 3
CodePudding user response:
You don't consider that "cat can catch mice" should report only 1 occurrence of "cat".
Try this (or something like it):
void firstWord( char st3[] ) {
char *firstw "EMPTY STRING";
int count = 0;
for( char *ptr = st3; ( ptr = strtok( ptr, " .,-" ) ) != NULL; ptr = NULL )
if( count == 0 ) {
firstw = ptr;
count ;
} else if( strcmp( ptr, firstw ) == 0 )
count ;
printf( "First word %s is repeated %d times\n", firstw, count );
}