首页 > 试题广场 >

统计单词

[编程题]统计单词
  • 热度指数:17596 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
编一个程序,读入用户输入的,以“.”结尾的一行文字,统计一共有多少个单词,并分别输出每个单词含有多少个字符。 (凡是以一个或多个空格隔开的部分就为一个单词)

输入描述:
输入包括1行字符串,以“.”结束,字符串中包含多个单词,单词之间以一个或多个空格隔开。


输出描述:
可能有多组测试数据,对于每组数据,
输出字符串中每个单词包含的字母的个数。
示例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;
}

发表于 2018-07-21 11:39:01 回复(2)
#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;
}

发表于 2021-03-04 16:32:28 回复(0)
#include<stdio.h>//直接从头开始num=0遇到空格或者.输出个数num=0
#include<string.h>
int main()
{
    char a[10000];gets(a);
    int i,num=0;
    for(i=0;i<strlen(a);i++)
    {
        if(a[i]!=' '&&a[i]!='.') num++;
        else
        {printf("%d ",num);num=0;}
    }
}

编辑于 2020-03-28 21:30:24 回复(0)

Once AC
#include <bits/stdc++.h>
using namespace std;
int main(){
    char ch;
    int sum=0;
    while((ch=cin.get())!='.'){
        if(ch==' '){
            cout<<sum<<" ";
            sum=0;
            continue;
        }
        sum+=1;
        if(cin.peek()=='.') cout<<sum;
    }
    return 0;
}

发表于 2020-03-11 18:26:30 回复(0)
论strtok的应用。
#include <stdio.h>
#include <string.h>
int main()
{
    char s[100],*t;
    while(gets(s))
    {
        t=strtok(s," .");
        do{
            printf("%d ",strlen(t));
        }while(t=strtok(NULL," ."));
        printf("\n");
    }
}


发表于 2020-02-14 15:33:02 回复(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;
			}
		}
	}
}

发表于 2020-01-15 09:04:05 回复(0)
#include<stdio.h>
#include<string.h>

int main() {
    char temp[100];
    int count[100];
    int word=0;
    while (scanf("%s", temp) != EOF) {
         count[word++]= strlen(temp);
    }
    word=word-2;
    for(int i=0;i<=word;i++){
        printf("%d ",count[i]);
    }
    printf("%d",count[++word]-1);
    return 0;
}
发表于 2019-03-12 19:58:44 回复(0)
print(*[len(i) for i in input().replace('.','').split()])

发表于 2018-07-05 01:45:48 回复(0)
#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+" ");
        }
    }
}

编辑于 2018-06-01 10:28:53 回复(0)
#include <stdio.h>
int main()
{int i,cnt=0;
 char s[100];
 while(gets(s)!=NULL){
    for(i=0;s[i]!='.';i++){
        if(s[i]!=' ') cnt++;
        if(s[i]==' '){if(cnt!=0) printf("%d ",cnt);
                      cnt=0;}
    }
    printf("%d\n",cnt);
    cnt=0;
 }
return 0;
}

发表于 2018-01-14 20:38:55 回复(0)
AC代码
#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;
}

发表于 2017-12-04 10:18:08 回复(0)
<?php
while($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";
  }

编辑于 2017-04-09 20:06:05 回复(0)
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    string s;
    while(cin>>s){
        if(s[s.size()-1]=='.')
            cout<<s.size()-1<<endl;
        else
            cout <<s.size()<< " ";

    }
    return 0;
}

应该是最短的AC代码。

编辑于 2017-03-12 16:49:52 回复(10)

Life is short , I use python.

One line is enough.

while True:
    try:
        print(" ".join(map(lambda c:str(len(c)),input().replace(".","").split())))
    except:
        break
发表于 2017-10-01 16:18:27 回复(0)
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string s;
    while (cin >> s) {
        if (s.at(s.length() - 1) == '.')
            cout << s.length() - 1 << endl;
        else
            cout << s.length() << " ";
    }
    return 0;
}
编辑于 2017-03-26 00:50:37 回复(1)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
    char s[1000];
    while(scanf("%s",s)!=EOF)
        {
        char *a=s;
        int count=0;
        while(*a!='.'&&*a!='\0')
        {
            count++;
            a++;
        }
       if(*a!='.')
            printf("%d ",count);
        else printf("%d\n",count);
        }
    return 0;
}
发表于 2017-12-30 21:19:07 回复(0)
#include <iostream>
#
include <cstdio>
using namespace std;
int main(){
    string str;
    int count=0;
    while(getline(cin,str)){
        for(int i=0;i<str.size();i++){
          if(str[i]!=' '&&str[i]!='.'){
              count++;
          }else{
              cout<<count<<" ";
              count=0;
          }
        }
    }return 0;
}
发表于 2020-03-09 22:18:55 回复(0)
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);
                }
            }
        }
    }
}

发表于 2018-10-08 10:22:23 回复(0)
#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;
}

发表于 2023-03-27 11:23:14 回复(0)
#include <iostream>
#include <vector>
using namespace std;

int main() {
    string s;
    while (getline(cin, s)) {
        int from, to; //记录单词开始、结束的位置,当然也可以直接设个int来记录长度
        vector<int> res;//每个单词的长度存到res中
        bool tag = true; //true:未进入单词模式,false:已进入单词模式
        for (int i = 0; i < s.size(); i++) {
            if (tag && isalpha(s[i])) { //如果未进入单词模式且该字符是字母
                tag = false; //进入单词模式
                from = i;
            }
            if (!tag && !isalpha(s[i])) { //如果已经在单词模式且该字符不是字母
                to = i;
                res.push_back(to - from); //保存单词长度
                tag = true; //退出单词模式
            }
        }
        for (int i = 0; i < res.size(); i++) {
            if (i != 0) {
                cout << " ";
            }
            cout << res[i];
        }
        cout << endl;
    }
}

发表于 2023-03-25 15:13:12 回复(0)