• Register
2 votes
14 views

Problem:

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

12 7 7
15,250 points

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 13 views
13 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 game 4.6k points
0 votes
1 answer 2 views
2 views
Problem : I have written a code in c++ to print all the permutation of a string. The code isrunning successfully but, does not show any result on the output page. Here is the code: using namespace std; void RecPermute(string, string); int main() { RecPermute("", "abc"); ... = rest.substr(0, i) + rest.substr(i+1); RecPermute(next, remaining); } } } Can anyone help me to correct my mistake?
asked Oct 8 sikandar 2.4k points
0 votes
1 answer 13 views
13 views
I have an array of string, in which I want to store sentences. These sentences contain variables, how do I tell C ++ that within the sentences there are variables? I don't want to print them on screen, so options like cout or printf don't work for me. I just want them to be ... ; record [0] = "Withdrawn" << withdraw << "dollars from the ATM." << balance << "dollars left; cout << record [0]; }
asked Aug 14 game 4.6k points
0 votes
1 answer 5 views
5 views
Problem: I want to check the length of a vector that I initialized and the length of a vector that is initialized dynamically by the user at run time. How I can do that? If possible, please give the examples.
asked 4 days ago Sheeza 2.9k points
0 votes
1 answer 5 views
5 views
I am trobuleing to find the length of array in c without using sizeof.
asked Oct 3 Daniel Anderson 4k points