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; }
This comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
DeleteLove the content guys, wish you wouldn't post under alts. But codes on as u may...
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteOkay, so, code reuse?
ReplyDeletecout<<"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++?
Or, store the input in a std::string and use size() to get the length? This is wrong on so many levels
ReplyDeleteWhy use a O(1) algorithm when you can write your own AND it's O(n).
ReplyDeleteWould take this lesson on string theory again, helped me alot.
ReplyDelete