当前位置:网站首页>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);
}边栏推荐
- 7-22 tortoise and rabbit race (result oriented)
- 6-9 statistics of single digits (15 points)
- SSH access control, blocking the IP when logging in repeatedly to prevent brute force cracking
- 7-6 mixed type data format input
- Tonybot humanoid robot infrared remote control play 0630
- Learn to punch in today
- Puzzle (016.3) is inextricably linked
- C library function - qsort()
- Statistical capital consonants
- String reverse order
猜你喜欢

Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion

Tailing rushes to the scientific and Technological Innovation Board: it plans to raise 1.3 billion, and Xiaomi Changjiang is the shareholder

US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars

puzzle(016.3)千丝万缕

Why is this error reported when modifying records in the database

Comprehensive evaluation of good-looking, easy-to-use and powerful handwriting note taking software: notability, goodnotes, marginnote, handwriting, notes writers, collanote, collanote, prodrafts, not

Leetcode(4)——尋找兩個正序數組的中比特數

中感微冲刺科创板:年营收2.4亿净亏1782万 拟募资6亿

Tiantu investment sprint Hong Kong stocks: asset management scale of 24.9 billion, invested in xiaohongshu and Naixue
![[qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?](/img/f6/fe61c84f289f0e74a45946dac687a6.jpg)
[qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?
随机推荐
洛谷P3065 [USACO12DEC]First! G 题解
分布式事务(Seata) 四大模式详解
必贝特医药冲刺科创板:年营收97万亏损1.37亿 拟募资20亿
Common commands for getting started with mongodb database
etcd集群权限管理和账号密码使用
Niuke: crossing the river
Leetcode (4) - - trouver la médiane de deux tableaux ordonnés positifs
Sendmail无法发送邮件及发送过慢解决
7-19 check denomination (solve binary linear equation)
牛客网:过河卒
Use of constraintlayout
7-23 currency conversion (using array conversion)
7-20 print 99 formula table (format output)
别再问自己适不适合做软件测试了
Thread. Sleep and timeunit SECONDS. The difference between sleep
556. 下一个更大元素 III : 简单构造模拟题
Puzzle (016.3) is inextricably linked
Tonybot humanoid robot checks the port and corresponds to port 0701
Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
表单文本框的使用(一) 选择文本