• Register
2 votes
22 views

Problem:

How can I find the length of a string in c++?

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Solution:

There are several ways to find the length of a string in c++

  1. Using string::size

  2. Using string::length

  3. Using C library function strlen() method

  4. Using while loop

  5. Using for loop

I am trying to put all methods in my single program for your better understanding.

#include <iostream> 
#include <string.h> 
using namespace std; 
int main() 
{ 
    // String obj 
    string str = "Hello World!"; 
  
    // 1. size of string object using size() method 
    cout << str.size() << endl; 
  
    // 2. size of string object using length method 
    cout << str.length() << endl; 
  
    // 3. size using old style 
    // size of string object using strlen function 
    cout << strlen(str.c_str()) << endl; 
  
    // The constructor of string will set it to the 
    // C-style string, 
    // which ends at the '\0' 
  
    // 4. size of string object Using while loop 
    // while 'NOT NULL' 
    int i = 0; 
    while (str[i]) 
        i++; 
    cout << i << endl; 
  
    // 5. size of string object using for loop 
    // for(; NOT NULL  
    for (i = 0; str[i]; i++) 
        ; 
    cout << i << endl; 
  
    return 0; 
} 

Every time you’ll get the output: 12

Thanks

 

13 9 6
94,240 points

Related questions

0 votes
1 answer 1 view
1 view
Problem: I need assistance in resolving this problem: how to get the length of a string in c
asked 2 days ago rakib1 50.4k points
0 votes
1 answer 1 view
1 view
Problem: Can someone please help this, I am new in c++ Find length of string in c++
asked 6 days ago Rohit kr 14.7k points
0 votes
2 answers 7 views
7 views
Problem Can any idea how i define length of string in c++
asked Mar 26 Subhendu 4.1k points
0 votes
1 answer 13 views
13 views
Problem: How do you find the length of a string? How do you find the length of a string without using strlen in C++? What is size of string in C++? Find length of string without using any library or built-in function like strlen() using strlen() function using Pointer
asked Dec 26, 2020 Mashhoodch 13k points
0 votes
1 answer 2 views
2 views
Problem Which function is batter way to find the size of a string in c++ size()/length(), Please anyone clear my confusion
asked 2 days ago Subhendu 4.1k points
0 votes
1 answer 2 views
2 views
Problem: Please help me fix this dilemma: how to get string length in c
asked 2 days ago rakib1 50.4k points
1 vote
1 answer 8 views
8 views
Problem: I new in C Programming. I want to know. What is a way in C that someone could find the length of a character array? I will happily accept pseudo-code, but am not averse to someone writing it out if they'd like to. Thanks.
asked Mar 21 Shovo210 13.2k points
0 votes
1 answer 19 views
19 views
I want to modify the length of a vector and at the same time eliminate the elements that remain if the new length is less than the original length of the vector. I don't know if there will be reserved words in the language to do this. It is with this type of vector: std :: vector name [];
asked Aug 17, 2020 game 4.6k points
0 votes
1 answer 2 views
2 views
Problem Anyone can help me how can i get pointer type char length in c++
asked 2 days ago Subhendu 4.1k points
0 votes
1 answer 1 view
1 view
Problem: I will request someone to answer my question Please, I have searched on the web but I got no useful information for this, now I am in difficulty continuing my learning
asked Mar 29 tuhin1 46.7k points