当前位置:网站首页>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 :
5Ideas : 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);
}边栏推荐
- Preliminary summary of structure
- MongoDB索引
- FPGA blocking assignment and non blocking assignment
- Tonybot humanoid robot starts for the first time 0630
- Exercise 8-8 moving letters
- 分布式事务(Seata) 四大模式详解
- Find specified characters
- 556. The next larger element III
- Luogu p5536 [xr-3] core city solution
- US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars
猜你喜欢

7-15 calculation of PI

Tiantu investment sprint Hong Kong stocks: asset management scale of 24.9 billion, invested in xiaohongshu and Naixue

一文了解微分段应用场景与实现机制

Understanding of closures

Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块

Paper sharing: generating playful palettes from images

MySQL multi table query subquery

Use of constraintlayout

天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库

Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
随机推荐
GRPC的四种数据流以及案例
Ultra simple mobile map development
SSH access control, blocking the IP when logging in repeatedly to prevent brute force cracking
Jiuyi cloud black free encryption free version source code
Accelerating strategy learning using parallel differentiable simulation
7-19 check denomination (solve binary linear equation)
tonybot 人形机器人 定距移动 代码编写玩法
洛谷P3065 [USACO12DEC]First! G 题解
Showmebug entered Tencent conference, opening the era of professional technical interview
Puzzle (016.3) is inextricably linked
Common commands for getting started with mongodb database
7-20 print 99 formula table (format output)
Solr series of full-text search engines - basic principles of full-text search
556. 下一个更大元素 III
J-luggage lock of ICPC Shenyang station in 2021 regional games (simple code)
Some concepts about agile
Luogu p5194 [usaco05dec]scales s solution
论文分享:Generating Playful Palettes from Images
Statistical capital consonants
添加Zabbix计算类型项目Calculated items