问题 B: 字符串的长度

题目描述

输入一些字符串,输出它们的长度。

输入

输入为多行。第一行N>0表示有N个测试用例,后面有N行,每行包含一个字符串(不超过1000个字符)。

输出

输出为多行,每行对应于一个测试用例。每行的格式为:

case i:length=j.

其中i表示测试用例编号(从1开始),j表示相应的字符串长度。

样例输入

4
I love China!
Do you want to pass this examination?
You will succeed finially!
Wish you succeed!

样例输出

case 1:length=13.
case 2:length=37.
case 3:length=26.
case 4:length=17.

c语言代码:

#include<stdio.h>
#include<string.h>
int main()
{
	int n;
	scanf("%d",&n);
	getchar();
	char a[1000] = {"\0"};
	for(int i=0;i<n;i++)
	{
		scanf("%[^\n]",a);
		getchar();
		printf("case %d:length=",i+1);
		printf("%d.\n",strlen(a));
	}
	return 0;
}

c++代码:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	int n;
	cin>>n;
	cin.get();
	string s;
	for(int i=0;i<n;i++)
	{
		getline(cin,s);
		cout<<"case "<<i+1<<":"<<"length="<<s.length()<<"."<<endl;
	}
	return 0;
}
发布了99 篇原创文章 · 获赞 63 · 访问量 6224

猜你喜欢

转载自blog.csdn.net/m0_43456002/article/details/102969870