当前位置:网站首页>MOOC week 8 programming exercise 1

MOOC week 8 programming exercise 1

2022-06-13 09:25:00 Hezeze

1 Word length (4 branch )
Topic content :

Your program reads a line of text , There are several words separated by spaces , With ‘.’ end . You want to output the length of each word in this line of text . The words here have nothing to do with language , It can include various symbols , such as “it’s” Count a word , The length is 4. Be careful , Consecutive spaces may appear in the line .

Input format :

Input gives one line of text in one line , With ‘.’ end , The ending period cannot be counted within the length of the last word .

Output format :

Output the length of the word corresponding to this line of text in one line , Each length is separated by a space , There is no final space at the end of the line .

sample input :

It’s great to see you here.

sample output :

4 5 2 3 3 4

The time limit :500ms Memory limit :32000kb

#include <stdio.h>

int main() {
    
    char text[100];
    gets(text);     // Input string 
    int i = 0;
    int count = 0;
    int flag = 1;
    int a[10];      // Array for counting word length 
    int x = 0;
    while (text[i] != '.') {
    
        if (text[i] != ' ') {
    
            flag = 0;
            count++;
        } else {
    
            flag = 1;
        }
        if (flag && count != 0 || text[i+1] == '.') {
       // To satisfy multiple spaces and only enter . Judgment of time 
            a[x] = count;
            count = 0;
            x++;
        }
        i++;
    }if (flag == 0) {
    
        printf ("%d", a[0]);

    }
    for (i = 1; i < x; i++) {
    
        printf (" %d", a[i]);
    }
    return 0;
}
原网站

版权声明
本文为[Hezeze]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270531584864.html