Is there a command in C that calculates how many words in a variable string?

Like for example strlen(); which calculates the number of characters in the string.

I need to count the number of words in the string, which will introduce the user in the terminal, then to withdraw the beginning of each word.
For example user enters: Sasha Kozlov and the program outputs the SK.

For example when you declare the main function:
int main (int argc, string argv[]) { }
also as int argc, calculates the number of arguments the user entered when the program starts I need the number of words entered by the user in the terminal with the function getstring().
June 14th 19 at 18:12
2 answers
June 14th 19 at 18:16
Pass the loop over all symbols, and look for the letters that are after the spaces
Already did
string name = get_string();
 int arg = 0;
 int len = strlen(name);
 for (int i = 0; i < len; i++) 
{
 if (name[i] == 32) 
arg++;
}
 arg++;
- Woodrow.Little31 commented on June 14th 19 at 18:19
not the spaces are considered, and the letters after the spaces. And no need to count print immediately. - nicklaus commented on June 14th 19 at 18:22
Make a word counter is initially equal to one, then after you will see a space added 1. That number of words. Here is a simple algorithm, he certainly does not account for cases when the string is multiple spaces in a row, a section of words is by using tabs, spaces at the end of words. - Woodrow.Little31 commented on June 14th 19 at 18:25
,
string name = get_string();
 int len = strlen(name);

 if (name[0] > 96)
{
 printf("%c", name[0] - 32);
}
 else 
{
 printf("%c", name[0]);
}

 for (int i = 0; i < len; i++)
{
 if (name[i] == 32)
{
 if (name[i + 1] > 96)
{
 printf("%c", name[i + 1] - 32);
}
 else 
{
 printf("%c", name[i + 1]);
}
}
}
 printf("\n");
- demetrius.Bedn commented on June 14th 19 at 18:28
First tried to solve the problem differently and for this I needed a word count in the line, and why I wrote what I need, but then I realized what can be done differently and wrote it. - Dianna_Tremblay1 commented on June 14th 19 at 18:31
something like this - Woodrow.Little31 commented on June 14th 19 at 18:34
good. You can still replace the test if (name[i + 1] > 96) on if (islower(name[i + 1]))and name[i + 1] - 32 on toupper(name[i + 1]). For this to work you need to add #include <ctype.h></ctype.h> - Woodrow.Little31 commented on June 14th 19 at 18:37
Thank you all for the advice - Woodrow.Little31 commented on June 14th 19 at 18:40
June 14th 19 at 18:14
\0 is always the same - it's a symbol of the end of the row.
But counting features any wishlist, including the number of words - no. Because the language is not necessary. You need - you do the math.
I need to count the number of words in the string, which will introduce the user in the terminal, then to withdraw the beginning of each word.
For example user enters: Sasha Kozlov and the program outputs the SK.

For example when you declare the main function:
int main (int argc, string argv[]) { }
also as int argc, calculates the number of arguments the user entered when the program starts I need the number of words entered by the user in the terminal with the function getstring(). - Woodrow.Little31 commented on June 14th 19 at 18:17
, well? Sit down and write code, once you need. - nicklaus commented on June 14th 19 at 18:20

Find more questions by tags CC++