C 循环统计输入的单词个数和字符长度

#include <stdio.h>
#include <Windows.h>

int main(void) {
    char word[128];
    int count = 0;
    int length = 0;

    printf("请输入任意多个单词:\n");

    while (1) {
        if (scanf("%s",word) != -1) {
            count++;
            length += strlen(word);
        } else {
            break;
        }
    }

    printf("你输入的单词个数为:%d\n", count);
    printf("你输入的字符长度为:%d\n", length);

    system("pause");
    return 0;
}

原文地址:https://www.cnblogs.com/tanghaiyong/p/11331282.html

时间: 2024-08-25 06:54:46

C 循环统计输入的单词个数和字符长度的相关文章

统计输入的单词中有几个长度大于n的,n是自己指定的,用函数对象实现

#ifndef COUNT_WORD_H #define COUNT_WORD_H #include <string.h> #include <iostream> #include <iterator> #include <vector> #include <algorithm> class GT_cls{ public: GT_cls(size_t val = 0) :bound_(val){} bool operator()(const st

【C语言】输入一个字符串,统计其中的单词个数,将第一个单词的首字母改为大写,并输出改写后的字符串

#include<stdio.h> int main() { char a[100]; int i, j=1; printf("请输入一串字符:"); gets_s(a); for (i = 0; a[i] != '\0'; i++)/*找出单词个数*/ { if (a[i] == ' ') j += 1; } printf("单词个数:%d\n", j); if (a[0] >= 'a' && a[0] <= 'z')/*判

C++ 统计用户输入的总行数和字符长度

#include <iostream> #include <Windows.h> #include <string> using namespace std; int main(void) { string line; int count = 0; int length = 0; cout << "请输入任意多行:" << endl; while (1) { // 如果遇到文件结束符, (cin的文件结束符是Ctrl+z),

统计不同的单词个数。

Problem Description lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数.下面你的任务是帮助xiaoou333解决这个问题. Input 有多组数据,每组一行,每组就是一篇小文章.每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束. Output 每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数. Sample Input you are my friend # Sample Outp

1219: 统计字符串的单词个数

题目描述 输入一行字符,统计并输出其中有多少个单词,单词之间用空格分隔. 输入 只有一行,保证只包含可见字符,且此行的所有字符数不超过100. 输出 一个整数,表示输入的一行字符中共有多少个单词.请注意行尾输出换行. 样例输入 I am a program. 样例输出 4 1 #include<iostream> 2 #include<cstdio> 3 #include<string.h> 4 using namespace std; 5 int main(){ 6

统计文件中单词个数

import sys import string #import collections if len(sys.argv) == 1 or sys.argv[1] in {"-h", "--help"}: print("usage: uniqueword filename_1 filename_2 ... filename_n") sys.exit() else: words = {} # words = collections.defaultd

循环-06. 统计一行文本的单词个数

循环-06. 统计一行文本的单词个数(15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 张彤彧(浙江大学) 本题目要求编写程序统计一行字符中单词的个数.所谓“单词”是指连续不含空格的字符串,各单词之间用空格分隔,空格数可以是多个. 输入格式: 输入给出一行字符. 输出格式: 在一行中输出单词个数. 输入样例: Let's go to room 209. 输出样例: 5 1 #include<stdio.h> 2 #incl

循环-06. 统计一行文本的单词个数(15)

1 #include<iostream> 2 #include<string> 3 using namespace std; 4 int main(){ 5 string s; 6 int i,c=0; 7 getline(cin,s); 8 for(i=1;i<s.length()-1;i++) 9 if(s[i]==' '&&s[i-1]!=' ') 10 c++; 11 if(s[i]!=' ') 12 c++; 13 cout<<c<

统计字符串中单词的个数

1.单纯统计单词个数,单词与单词之间只考虑空格的情况 // word_statistic.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include <string> using namespace std; #define M 10000 #define N 20 int _tmain(int argc, _TCHAR* argv[]) { char str1[M]={0};