两种C++测字符串长度的代码:

法一:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string tStr;
    cin>>tStr;
    int tLen    = tStr.length();
    cout<<tLen<<endl;
    system("pause");
    return 0;
}

法二:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    int i;
    string tStr;
    cin>>tStr;
    int tCount = 0;
    while(tStr[i]!='\0')
    {
        tCount++;
        i++;                  
    }
    cout<<tCount;
    system("pause");
    return 0;
}

法三:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string tStr;
    cin>>tStr;
    int i=0;
    loop:if(tStr[i]!='\0')
    {
        i++;
        goto loop;                      
    }
    cout<<i<<endl;
    system("pause");
    return 0;

}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-01
  • 2022-12-23
  • 2022-12-23
  • 2021-06-17
  • 2021-10-05
  • 2021-06-24
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-19
  • 2022-12-23
  • 2022-12-23
  • 2021-09-18
  • 2022-12-23
相关资源
相似解决方案