C++ Lanuage | String Counts

Tuesday 11 June 2013

Have you tried counting string inputs manually? Isn't it difficult? That is why I made a program that enables a user to input his desired string length and string name then activates the program to count how many characters are there in the input.

We all know that a string is a random inputs of different characters. Today, I'm about to show you how to count characters in a srtring input. This program is used with the string length statement which enables the program to count how many characters are there in a string. Check out the sample code below.

#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <string.h>

int main()
{
    char string[127];
   
    std::cout<<"Enter a string: ";

    std::cin.get(string,127);
   
    std::cout<<"\nString has "<<strlen(string)<<" characters.";
   
    getch();
}

The program runs like this. At first it will ask the user to input any characters he want and then the input will be stored in the declared string as seen above. The declared string has a limit of 127 counts. After the input, your string will now have their labeled numbers with them. It also acts like a memory count. After that, the last cout statement will call on the last number where the last character is stored using the strlen statement (string length statement). Then it will show you the result. You can now terminate the program. Check out the sample output below.


Hopefully this program helped you a lot. Thanks!

No comments:

Post a Comment

Please do leave a comment. It's gonna be a friendly conversation between programmers. Hope you do find our blog helpful. Thank you!

Related Posts Plugin for WordPress, Blogger...