输入字符串,用c++实现计算并显示字符串的长度,这一操作反复进行,直到输入exit为止

来源:百度知道 编辑:UC知道 时间:2024/06/16 04:01:45
一定要用c++实现!!!不是c语言!

#include<iostream>
#include<cstring>
using namespace std;
void main()
{
char *str=new char[100];
char ch[5]="exit";
cout<<"input the string:"<<endl;
cin>>str;
do{
int len=strlen(str);
cout<<"it's length is:"<<endl;
cout<<len<<endl;
cout<<"input the string:"<<endl;
cin>>str;
}while(strcmp(str,ch)!=0);
}

//可以输入空格,最大长度本例为1000,你也可以通过修改MAX_SIZE值改变长度

#include <iostream>
using namespace std;

int main()
{
const int MAX_SIZE = 1000;
char array[MAX_SIZE];
char arr[] = "exit";

while(1)
{
cout << "请输入一个字符串:";
cin.getline(array,MAX_SIZE, '\n');
if (strcmp(array, arr) == 0) break;
cout << "字符串的长度是" << strlen(array) << endl;