Monday, 8 September 2014

Count Number of Character in String

Structure of the Problem Requirements 

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
Count Number of Characters in C++ Program

Share it Please
asad

About Author!

Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.

10 comments: Post Yours! Read Comment Policy!▼
Important Note:
We have Zero Tolerance to Spam. Chessy Comments and Comments with Links will be deleted immediately upon our review.

  1. Im assuming english is not your first language?

    ReplyDelete
    Replies
    1. our language is just coding and programming :) . isn't Uncle_RUCKUS :)

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

      Delete
    3. Having a look at the other posts on the site, it almost certainly definitely is nothing but jokes.
      http://www.ancodingpoint.com/2014/09/typedef-in-c.html

      Delete
  2. I hope you understand that your spikes in hits lately is due to redditors coming to ridiculing your content. It's not good press at all.

    ReplyDelete
  3. I'm still really hoping these guys are just joking.

    ReplyDelete
  4. 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
  5. Or, store the input in a std::string and use size() to get the length? This is wrong on so many levels

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

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

    ReplyDelete