当前位置:网站首页>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);
}边栏推荐
- Accelerating strategy learning using parallel differentiable simulation
- Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion
- 使用并行可微模拟加速策略学习
- 中感微冲刺科创板:年营收2.4亿净亏1782万 拟募资6亿
- MongoDB索引
- Tailing rushes to the scientific and Technological Innovation Board: it plans to raise 1.3 billion, and Xiaomi Changjiang is the shareholder
- 7-4 BCD decryption (10 points)
- Eight sorts
- Selective sorting
- Polestar美股上市:5.5万台交付如何支持得起超200亿美元估值
猜你喜欢

MySQL multi table query subquery

retrofit

adc128s022 ADC verilog设计实现

puzzle(016.4)多米诺效应

Showmebug entered Tencent conference, opening the era of professional technical interview

Accelerating strategy learning using parallel differentiable simulation

Paper sharing: generating playful palettes from images

如何查询淘宝天猫的宝贝类目

Niuke: crossing the river

7-18 finding the single root of polynomial by dichotomy
随机推荐
洛谷P4047 [JSOI2010]部落划分 题解
China PETG market forecast and Strategic Research Report (2022 Edition)
别再问自己适不适合做软件测试了
ZABBIX saves the page blank after adding calculated items
7-6 mixed type data format input
pyQt界面制作(登录+跳转页面)
Luogu p4047 [jsoi2010] tribal division solution
tonybot 人形機器人 紅外遙控玩法 0630
Polestar美股上市:5.5万台交付如何支持得起超200亿美元估值
Learn to punch in today
C library function - qsort()
Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion
Sword finger offer 28 Symmetric binary tree
556. 下一个更大元素 III
FPGA blocking assignment and non blocking assignment
Paper sharing: generating playful palettes from images
【7.3】146. LRU缓存机制
提高效率 Or 增加成本,开发人员应如何理解结对编程?
Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
Statistical capital consonants