c++ sizeof( string )

后端 未结 8 1527
陌清茗
陌清茗 2020-12-09 13:59
#include 
#include 

int main(int argc, char *argv[])
{
   cout << "size of String " << sizeof( string );
               


        
8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 14:50

    you can also use strings and can find out its length by string.length() function. look at the below code:

    // Finding length of a string in C++
    
    #include
    #include
    using namespace std;
    
    int count(string);
    
    int main()
    {
    string str;
    cout << "Enter a string: ";
    getline(cin,str);
    cout << "\nString: " << str << endl;
    cout << count(str) << endl;
    
    return 0;
    
    }
    
    int count(string s){
    if(s == "")
      return 0;
    if(s.length() == 1)
      return 1;
    else
        return (s.length());
    
    }
    

    you can get the details from : http://www.programmingtunes.com/finding-length-of-a-string-in-c/

提交回复
热议问题