C++ Program to Count Number of Words in a String

By | 26.12.2016

Count Number of Words in a String


Write a C++ Program to Count Number of Words in a String. Here’s simple C++ Program to Count Number of Words in a String in C++ Programming Language.


C++ Strings : 


String is a sequence of characters. char data type is used to represent one single character in C++. So if you want to use a string in your program then you can use an array of characters.

The declaration and definition of the string using an array of chars is similar to declaration and definition of an array of any other data type.

Any string ends with a terminating null character ‘\0’. An array definition in such a way should include null character ‘\0’ as the last element.

C++ provides following two types of string representations:

  • The C-style character string.
  • The string class type introduced with Standard C++.

The C-style character string originated within the C language and continues to be supported within C++. This string is actually a one-dimensional array of characters which is terminated by a null character ‘\0’. Thus a null-terminated string contains the characters that comprise the string followed by a null.


Here is source code of the C++ Program to Count Number of Words in a String. The C++ program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.


SOURCE CODE : :


/*  C++ Program to Count Number of Words in a String  */

#include<iostream>
#include<string.h>
using namespace std;

int main()
{
        char strs[100], countw=0, strw[15], i;
        cout<<"\nEnter any string :: ";
        gets(strs);

        int len=strlen(strs);

        for(i=0; i<len; i++)
        {
                if(strs[i]==' ')
                {
                        countw++;
                }
        }
        cout<<"\nTotal Number of Words in a String is :: "<<countw+1<<"\n";

        return 0;
}

OUTPUT : :


/*  C++ Program to Count Number of Words in a String  */

Enter any string :: This is a test String

Total Number of Words in a String is :: 5

Process returned 0

Above is the source code for C++ Program to Count Number of Words in a String which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

4 6 votes
Article Rating
Category: C++ Programming String Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
moaaz

//may be there some space by the end of the string

void countWordsInString() {
char str[100];
cout << “enter string: “;
gets_s(str);
int i = 0;
int cn = 1;
while (str[i] != ‘\0′) {
if (str[i]==’ ‘) {
//may be there some space by the end of the string
if (str[i + 1] != ‘\0’) {
++cn;
}

}
++i;
    }
cout << cn << ” words”;
}