• Register
2 votes
15 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 7 views
7 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 5.6k points
0 votes
1 answer 18 views
18 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 12 views
12 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, 2020 sikandar 2.4k points
0 votes
1 answer 15 views
15 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, 2020 game 4.6k points
0 votes
1 answer 16 views
16 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 Oct 21, 2020 Code Learner 5.1k points
0 votes
1 answer 14 views
14 views
I am trobuleing to find the length of array in c without using sizeof.
asked Oct 3, 2020 Daniel Anderson 4k points
0 votes
1 answer 26 views
26 views
Is there a way to find how many values an array has? Detecting whether or not I have reached the end of an array would also work.
asked Aug 8, 2020 game 4.6k points
0 votes
2 answers 19 views
19 views asked Oct 17, 2020 SapperIt 1.2k points
2 votes
1 answer 25 views
25 views
Problem: Hello all, I am a student of C++ programming. I am wondering if there's an abbreviation or a more elegant way of getting the last character of a string. Let&rsquo;s assume, the given string is &ldquo;I am a String&rdquo; then the output should be ... ; I&rsquo;ve almost 30 minutes for the problem and couldn&rsquo;t figure out any legit solution. I hope you people would be helpful. Thanks.
asked Apr 18, 2020 Gavin 15.3k points
0 votes
1 answer 9 views
9 views
Problem Hey, I just want to know how to reverse each word in a string using C++. provide me an approach to make things clear.
asked Dec 19, 2020 chris jordan 2.1k points