Hello
I would like to count how many words are in a string
ie "PETER JOHNSON LTD"
so this string contains 3
I was going to use strtok but this is too slow.
Any other ideas?
Hello
I would like to count how many words are in a string
ie "PETER JOHNSON LTD"
so this string contains 3
I was going to use strtok but this is too slow.
Any other ideas?
Go over the string and count spaces. The number of words is one more than the number of spaces in the string if the string isn't empty.
#include <stdio.h>
#include <ctype.h>
int main( void ) {
char string[100];
int i;
int count = 0;
printf( "Enter a sentence> " );
fgets( string, 100, stdin );
string[strlen( string ) - 1] = '\0';
/* count the words */
for ( i = 0; string[i]; ++i ) {
if ( isspace( string[i] ) ) {
++count;
}
}
if ( i > 0 ) {
++count;
}
printf( "\"%s\" has %d words\n", string, count );
return 0;
}
>>Go over the string and count spaces
Generally, yes, but you also have to check if words are separated by more than one white space (spaces or tabs). When a white space character is encountered you have to increment the loop counter until the first non-white space character or end-of-string is found before continuing the loop. If you don't, the word count might be wrong.
Try something like this. It would work for any kind of whitespaces (newline, spaces, tabs).
int main(void)
{
char str[] = " hello \n to all \n\r\f the \t people ";
int limit = strlen(str);
int i, change = 1, words = 0;
for(i = 0; i < limit; ++i)
{
if(!isspace(str[i]))
{
if(change)
{
++words;
change = 0;
}
}
else
{
change = 1;
}
}
printf("Word count: %d\n", words);
getchar();
return(0);
}
both are nice answers...
I'd use strtok(). If it is slower, it isn't much slower. It is also neater, seeing as how it won't take as many lines of code and variables.
Just in case this problem has been bugging you after the past 2 and a half years.
Here's mine
int wordCount( char *line )
{
int count = 0 ;
// advance until the first nonwhitespace character
while( isspace( *line ) ) line++ ;
int len = strlen( line ) ;
bool onWord = true ;
for( int i = 0 ; i < len; i++ )
{
if( isspace( line[i] ) )
{
// INcrement count only when
// stepping OFF word to whitespace
if( onWord )
count++ ;
onWord = false ;
}
else
{
onWord = true ;
}
}
// If you ended while still on word
// then increment count here to count
// the "last" word
if( onWord )
count++ ;
return count ;
}
int main()
{
char *line1 = "a line of words" ;
char *line2 = "Hi ther e !! Each glob of characters\n"
"counts as a word \t \t \t \n" ;
char *line3 = "Here's another long line of 3 4 6 6 8 " ;
printf( "%d words in `%s`\n\n", wordCount(line1), line1 ) ;
printf( "%d words in `%s`\n\n", wordCount(line2), line2 ) ;
printf( "%d words in `%s`\n\n", wordCount(line3), line3 ) ;
}
well, okay...... but we waited almost 3 years for that?
#include<stdio.h>
int main()
{
char name[20];
int i, j, count = 0, end = 0, line = 0, word = 0, flag = 0;
printf ( "Enter the strings\n" );
gets ( name );
for ( i = 0; name[i] != '\0'; i++ )
{
if ( name[i] == ' ' || name[i] == '\t' )
printf ( " " );
count++;
if ( name[i] == '\0' )
end = 0;
else if ( end == 0 )
{
end = 1;
line++;
}
if ( name[i] == ' ' || name[i] == '\t' || name[i] == '\n' )
flag = 0;
else if ( flag == 0 )
{
flag = 1;
word++;
}
}
printf ( "Total number of characters %d\n", count );
printf ( "Total number of word %d\n", word );
printf ( "Total number of line %d\n", line );
}
^^ At above poster. No.
alright, someone close this tard-magnet thread already.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.