当前位置:网站首页>7-3 count the number of words in a line of text
7-3 count the number of words in a line of text
2022-07-03 14:33:00 【Study hard 867】
This topic requests to write a program to count the number of words in a line of characters . So-called “ word ” A string that has no spaces in it , Separate the words with spaces , The number of spaces can be multiple .
Input format :
Type to give a line of characters .
Output format :
Output the number of words in a line .
sample input :
Let's go to room 209.
sample output :
5
Ideas : I originally wanted to count letters, but I saw that the required string on the topic was a string without spaces , in other words &*, This kind of strange characters , If you use something that is not a space, it is c!=' ', But if it is a blank space, you have to count , So let's look at the blanks , If the first letter appears after a space, it is a string , At the beginning, there was no ‘ ’, But it's also a string . Let's define a flag after the space appears , Let this sign become 1, The next time we encounter something that is not a space, we will add the count variable 1, Add in the count variable 1 Then let's change the space mark 0, As for the first part without spaces, it is also a string , We just need to make the logo initial 1 that will do , According to this idea, we write the following code .
Code :
#include <stdio.h>
#include <string.h>
int main(){
int number=0;
int i,flag=1;
char a[10000];
gets(a);
int len=strlen(a);
for(i=0;i<len;i++){
if(a[i]==' ')flag=1;
if(a[i]!=' '&&flag==1){
number++;
flag=0;
}
}
printf("%d",number);
}
边栏推荐
- etcd集群权限管理和账号密码使用
- 7-17 crawling worms (break exercise)
- 编程语言:类型系统的本质
- Learn to punch in today
- Tonybot humanoid robot infrared remote control play 0630
- SSH访问控制,多次失败登录即封掉IP,防止暴力破解
- retrofit
- Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
- 洛谷P5194 [USACO05DEC]Scales S 题解
- 洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
猜你喜欢
编程语言:类型系统的本质
protobuf与grpc
Leetcode(4)——尋找兩個正序數組的中比特數
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Eight sorts
Timecho of Tianmou technology completed an angel round financing of nearly 100 million yuan to create a native timing database of the industrial Internet of things
如何查询淘宝天猫的宝贝类目
分布式事务(Seata) 四大模式详解
天图投资冲刺港股:资产管理规模249亿 投了小红书与奈雪
必贝特医药冲刺科创板:年营收97万亏损1.37亿 拟募资20亿
随机推荐
Leetcode (4) -- find the median of two positively ordered arrays
表单文本框的使用(一) 选择文本
Exercise 8-8 moving letters
MongoDB数据库入门的常用命令
Common commands for getting started with mongodb database
Although not necessarily the best, it must be the hardest!
Pyqt interface production (login + jump page)
Doris学习笔记之数据表的创建
protobuf与grpc
Leetcode (4) - - trouver la médiane de deux tableaux ordonnés positifs
一文了解微分段应用场景与实现机制
Jiuyi cloud black free encryption free version source code
天图投资冲刺港股:资产管理规模249亿 投了小红书与奈雪
Find the sum of the elements of each row of the matrix
C language,%d% Difference between 2D%2d%02d
7-22 tortoise and rabbit race (result oriented)
7-16 find the set of integers that meet the given conditions
分布式事务(Seata) 四大模式详解
dllexport和dllimport
Understanding of closures