7-40单词长度

7-40 单词长度
你的程序要读入一行文本,其中以空格分隔为若干个单词,以.结束。
你要输出每个单词的长度。这里的单词与语言无关,可以包括各种符号,比如it’s算一个单词,长度为4。注意,行中可能出现连续的空格;最后的.不计算在内。
输入格式:输入在一行中给出一行文本,以.结束提示:用scanf("%c",…);来读入一个字符,直到读到.为止。
输出格式:在一行中输出这行文本对应的单词的长度,每个长度之间以空格隔开,行末没有最后的空格。
输入样例:It’s great to see you here.
结尾无空行输出样例:4 5 2 3 3 4

法一:


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String len = new String();
		len = sc.nextLine();
		String[] a;
		a = len.split(" ");
		int lem=len.split(" ").length;
		
		int[] count = new int[a.length];
		for(int i=0;i<a.length;i++) {
			if(a[i]!="") {
				for(int j=0;j<a[i].length();j++) {
					if(a[i].charAt(j)!='.') {
						count[i]++;
					}
				}
			}
		}
        int flag=0;
		for(int i=0;i<count.length;i++) {
			if(count[i]!=0) {
                if(flag==0)  
                {
                    flag=1;
                    System.out.printf("%d",count[i]);
                }
			    else System.out.printf(" %d",count[i]);
			}
		}
	}

}

法二:


import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        String s=input.nextLine();
        int len=s.length(),j=0;
        int a[]=new int[len];
        for(int i=0;i<len;i++){
                if(s.charAt(i)=='.') break;
                if(s.charAt(i)!=' '){
                    a[j]++;
                }else if(s.charAt(i)==' '){
                    j++;
                }
        }
        int count=0;
        for(int i=0;i<a.length;i++){
            if(a[i]!=0){
                if(count==0) {
                    System.out.print(a[i]);
                    count=1;
                }
                else System.out.print(" "+a[i]);
            }
        }
    }
}

法三:(还有漏洞哈,请指教错误!谢谢)


#include<bits/stdc++.h>
using namespace std;

int main()
{
    string s;
   int flag=0;
   while(cin>>s){
      if(flag==0){
          char cc=*s.rbegin();
          if(cc=='.') cout<<s.length()-1;
          else cout<<s.length();
          flag=1;
       }
      else{
          char c=*s.rbegin();
          if(c=='.') {
              cout<<" "<<s.length()-1;
          }
          else  cout<<" "<<s.length();
      }
   }
    return 0;
}

法四:


#include <stdio.h>
int main ()
{
    char ch;
    int count=0;//计数
    while(1)
    {
        int a=0;//记单词长度
        scanf("%c",&ch);
        while(ch!='\n'&&ch!=' '&&ch!='.')
        {
            a++;
            scanf("%c",&ch);
        }
        if(a!=0)
        {
            count++;
            if(count==1) printf("%d",a);
            else printf(" %d",a);
        }
        if(ch=='.') break;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值