输入包括1行字符串,以“.”结束,字符串中包含多个单词,单词之间以一个或多个空格隔开。
可能有多组测试数据,对于每组数据, 输出字符串中每个单词包含的字母的个数。
hello how are you.
5 3 3 3
#include<stdio.h>
int main()
{
char str[100];
int t, count;
while(gets(str) != NULL){
t = 0, count = 0;
while(str[t] != '\0'){
if(str[t] == '.')
printf("%d", count);
if(str[t] != ' ')
count++;
if(str[t] == ' '){
printf("%d ", count);
count = 0;
}
t++;
}
}
return 0;
}
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
//我的想法是用getchar函数消去输入时的空格,这样就能单独计算每个单词的字符数
int main(void)
{
string s;
while(cin >> s)
{
getchar();//用getchar()吃掉回车和空格
int count;
int i = 0;
int pos = s.size();
char c = s[pos - 1];
if(c == '.')//最后是“.”结尾当然就要少计数一个
count = -1;
else
count = 0;
sort(s.begin(),s.end());
unique(s.begin(), s.end());//去掉相邻的重复元素
while(s[i] != '\0')//单词计数
{
i++;
count++;
}
cout << count << ' ';
}
return 0;
} //还是注意一下边界值
#include<iostream>
#include<cstring>
using namespace std;
int main() {
char ch[1000];
while (cin.getline(ch,1000)) {
int len = strlen(ch);
for (int i = 0; i<len; i++) {
int temp=0;
for (int j = 0; ch[i + j] != ' '&&i+j<len&&ch[i+j]!='.'; j++)
temp = j;
if (temp>0) {
cout << temp+1 << " ";
i += temp;
}
}
}
} #include<stdio.h>
int main(){ char s[1000]; int i=0; int c=0; gets(s); while(s[i]!='.'){ if((s[i]==' ' && s[i+1]!=' ')){ printf("%d ",c); c=0; }else{ if(s[i]!=' '){ c++; } if(s[i+1]=='.') printf("%d",c); } i++; } return 0;
}
//java篇
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner scanr=new Scanner(System.in);
String str=scanr.nextLine();
for (int i=0;i<str.split(" ").length;i++){
int len=str.split(" ")[i].length();
if (i==str.split(" ").length-1)
System.out.print((len-1));
else
System.out.print(len+" ");
}
}
}
#include <iostream>
#include <string>
using namespace std;
int main(){
string str;
while(cin>>str){
if(str[str.length()-1]=='.') cout<<str.length()-1<<endl;
else cout<<str.length()<<" ";
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main(){
string str;
while(getline(cin,str,'.')){
int count=0,i,j=0,a[100]={0};
for(i=0;i<=str.size();i++){
if(str[i]==' '||str[i]=='\0'){
a[j]=count;
j++;
count=0;
}else count++;
}
for(i=0;i<j;i++){
cout<<a[i];
if(i!=j-1) cout<<" ";
}
cin.get();
}
return 0;
}
<?phpwhile($str = fgets(STDIN)){$str = substr($str,0,strpos($str,'.'));$word_count = explode(" ",$str);$word_count = array_filter($word_count);//删除空元素$len = count($word_count);for($i=0;$i<$len-1;$i++){echo strlen($word_count[$i])." ";}echo (strlen($word_count[$i]));echo "\n";}
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.nextLine();
String[] strs = str.split(" ");
for(int i=0;i<strs.length;i++){
if(i != strs.length-1){
System.out.print(strs[i].length());
System.out.print(" ");
}else{
System.out.println(strs[i].length()-1);
}
}
}
}
}
#include <cstddef>
#include <string>
#include <iostream>
using namespace std;
int main()
{
int count=0;//记录长度
string s;
getline(cin,s);//如果使用cin的话会过滤掉空格这种字符
for(int i=0;i<s.size();i++)
{
if(s[i]==' '||s[i]=='.')//符合条件就输出
{
cout<<count<<" ";
count=-1;//输出一次置-1,因为下面紧接着就要加1
}
count++;
}
}
主要是注意cin会吞掉空格、回车等不可见字符,用getline函数避免即可 #include <iostream>
#include <cstring>
using namespace std;
int main() {
string s;
while (getline(cin, s)) {
int len = s.length();
int sum = 1;
for (int i = 0; i < len; i++) {
if (s[i] == ' ') {
sum++;
}
}
int a[20] = {0};
int x = 0;
for (int i = 0; i < len; i++) {
if (s[i] == '.')break;
if (s[i] != ' ') {
a[x]++;
} else {
x++;
}
}
for (int i = 0; i < sum; i++) {
cout << a[i] << " ";
}
cout << endl;
}
return 0;
}