Count Number of Character in String

In this problem we will learn how to count number of characters in C++ Programs. The program asked user to enter a string and then count the number of character include space and special characters. In this problem we used gets(str) functions which also count as a character . Here is the source code of this problem which help you in better understanding. 

Source Code 


#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
int main ()
{
 char story [150];
 int words =0;
 cout<<" \t \t \t LEP \n \n \n  ";
 cout<<" \t \t Enter Your Story  \n  ";
 cout<<endl;
 gets (story);
 for(int i =0; story[i]!='\0';i++)
 {
  words++;
 }
 cout<<endl;
 cout<<"Your Story Contain "<<words<<" Character"<<endl;
 
 return 0; 
 
}

Output of the Program

Count Number of Characters

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.

9 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
    Replies
    1. This comment has been removed by a blog administrator.

      Delete
  2. Love the content guys, wish you wouldn't post under alts. But codes on as u may...

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. Okay, so, code reuse?
    cout<<"Your Story Contain "<<words<<" Character"<<endl;
    Then there's the use of gets(). Two things wrong with that. First off, a short, slightly important snippet from man 3 gets:
    Never use gets()
    Secondly, why would you not take advantage of getline, which is actually part of C++?

    ReplyDelete
  6. Or, store the input in a std::string and use size() to get the length? This is wrong on so many levels

    ReplyDelete
  7. Why use a O(1) algorithm when you can write your own AND it's O(n).

    ReplyDelete
  8. Would take this lesson on string theory again, helped me alot.

    ReplyDelete