代码改变世界

C++连续字符串测长度

2013-04-13 13:26  wuzhang  阅读(751)  评论(0编辑  收藏  举报

两种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;

}