String Programming Example, C++ String, String Example
Write a C++ program to find the length of string.


Note: In this program, to enter string you can also use another function like cin.getline(string_name, string_size) in place of  cin.get(string_name, string_size)


#include<iostream>
using namespace std;
int main( )
{
     int length = 0; // variable to hold length of string 
     int i; // loop variable to count character in the string
    char name[30];
    cout<<"Enter string to find the length is : ";
    cin.get(name, 30);
    for(i = 0; name[i] != '\0'; i++)
{
   length = length + 1;
}
  cout << "Length of string is: " << i;
  return 0;
}


Program output is tested on www.jdoodle.com


Enter string to find the length is : Mukesh Kumar Rajput
Length of string is: 19

Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: