C++ Function to Count All the Words in a String

Counting words in a string - c programming

You needed

int words(const char sentence[])
{
}

(note braces).

For loops go with ; instead of ,.


Without any disclaimer, here's what I'd have written:

See it live http://ideone.com/uNgPL

#include <string.h>
#include <stdio.h>

int words(const char sentence[ ])
{
int counted = 0; // result

// state:
const char* it = sentence;
int inword = 0;

do switch(*it) {
case '\0':
case ' ': case '\t': case '\n': case '\r': // TODO others?
if (inword) { inword = 0; counted++; }
break;
default: inword = 1;
} while(*it++);

return counted;
}

int main(int argc, const char *argv[])
{
printf("%d\n", words(""));
printf("%d\n", words("\t"));
printf("%d\n", words(" a castle "));
printf("%d\n", words("my world is a castle"));
}

Count the number of all words in a string

You can use strsplit and sapply functions

sapply(strsplit(str1, " "), length)

C++ function to count all the words in a string

A less clever, more obvious-to-all-of-the-programmers-on-your-team method of doing it.

#include <cctype>

int CountWords(const char* str)
{
if (str == NULL)
return error_condition; // let the requirements define this...

bool inSpaces = true;
int numWords = 0;

while (*str != '\0')
{
if (std::isspace(*str))
{
inSpaces = true;
}
else if (inSpaces)
{
numWords++;
inSpaces = false;
}

++str;
}

return numWords;
}

Count words in a user-input string in C

It's just that you're counting spaces, this would also be incorrect if the user ended the string with a bunch of spaces. Try something like this:

#include <stdio.h>
#include <string.h>
void main()
{
char s[200];
int count = 0, i;
int foundLetter = False;
printf("enter the string\n");
gets(s);
for (i = 0;i<strlen(s);i++)
{
if (s[i] == ' ')
foundLetter = False;
else
{
if (foundLetter == False)
count++;
foundLetter = True;
}
}
printf("number of words in given string are: %d\n", count);
}

As other users have commented, your program is susceptible to many other issues depending on the string inputed. The example I have posted assumes that anything that is not a space is a letter and if you find at least one letter, than that's a word. Instead of boolean values you could use a counter to make sure that the word is at least a certain length. You could also check to see that it is not a number or symbol by either writing your own regex function or using an existing one. As others have said there is a lot more you can do with this program, but I've provided an example to point you in the right direction.

How to count how many word in string?

For starters you have to write the declarations at least like

char buff[] = "This is a real-life, or this is just fantasy";
const char *op = "is";

Also if you need to count words you have to check whether words are separated by white spaces.

You can do the task the following way

#include <string.h>
#include <stdio.h>
#include <ctype.h>

//...

size_t n = strlen( op );
for ( const char *p = buff; ( p = strstr( p, op ) ) != NULL; p += n )
{
if ( p == buff || isblank( ( unsigned char )p[-1] ) )
{
if ( p[n] == '\0' || isblank( ( unsigned char )p[n] ) )
{
count++;
}
}
}
printf("%d",count);

Here is a demonstration program.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(void)
{
char buff[] = "This is a real-life, or this is just fantasy";
const char *op = "is";
size_t n = strlen( op );

size_t count = 0;

for ( const char *p = buff; ( p = strstr( p, op ) ) != NULL; p += n )
{
if ( p == buff || isblank( ( unsigned char )p[-1] ) )
{
if ( p[n] == '\0' || isblank( ( unsigned char )p[n] ) )
{
count++;
}
}
}

printf( "The word \"%s\" is encountered %zu time(s).\n", op, count );

return 0;
}

The program output is

The word "is" is encountered 2 time(s).


Related Topics



Leave a reply



Submit