当前位置:网站首页>7-26 word length (input and output in the loop)
7-26 word length (input and output in the loop)
2022-07-01 08:07:00 【Big fish】
Your program reads a line of text , There are several words separated by spaces , With . end . You have to output the length of each word . The words here have nothing to do with language , It can include various symbols , such as it’s Count a word , The length is 4. Be careful , Consecutive spaces may appear in the line ; final . Don't count in .
Input format :
Input gives one line of text in one line , With . endTips : use scanf("%c",…); To read in a character , Until I read . until .
Output format :
Output the length of the word corresponding to this line of text in one line , Each length is separated by a space , There is no final space at the end of the line .sample input :
It’s great to see you here.sample output :
4 5 2 3 3 4
I've been stuck in the last test point , Very afflictive
Wrong code :
#include <stdio.h>
int main ()
{
char ch;
while(1)
{
int a=0;
scanf("%c",&ch);
while(ch!='\n'&&ch!=' '&&ch!='.') // When enter occurs , Space , This reading will end at the full stop
{
a++; // Count
scanf("%c",&ch); // Continue to enter the remaining letters of the word
}
if(a!=0) // This word has a length
{
printf("%d",a);
if(ch!='.') printf(" "); // If there is a space at the end, one more space will be output , Replace the space here with * It can be seen that
}
if(ch=='.') break; // After jumping out of the outermost loop , These are output together
}
return 0;
} Running results :
Then I asked the teacher , Find out : No, I didn 't . If a space is encountered before, a space will be output
Finally, it came out , The key is to add a counter count( In fact, it is only the first flag bit ).
Code :
#include <stdio.h>
int main ()
{
char ch;
int count=0;
while(1)
{
int a=0;
scanf("%c",&ch);
while(ch!='\n'&&ch!=' '&&ch!='.')
{
a++;
scanf("%c",&ch);
}
if(a!=0)
{
count++; // Just for the first time
if(count==1) printf("%d",a); // Output the first number without a space in front of it
else printf(" %d",a); // When outputting the remaining numbers, there is a space in front ( Replace this space with * It can be seen clearly )
}
if(ch=='.') break;
}
return 0;
}Ha ha ha ha ha ha ha ha ha !!!!!!

边栏推荐
- How to troubleshoot SharePoint online map network drive failure?
- 力扣每日一题-第32天-1822.数组元素积的符号
- Instead of houses, another kind of capital in China is rising
- 2022.6.30 省赛+蓝桥国赛记录
- Programmer's regimen
- [getting started] input n integers and output the smallest K of them
- Implementation and encapsulation of go universal dynamic retry mechanism
- go通用动态重试机制解决方案的实现与封装
- web254
- Saving db4i depth camera pictures with MATLAB
猜你喜欢

QT -- 1. QT connection database
![[MySQL learning notes 25] SQL statement optimization](/img/01/cf0556961670bb43512caab8d5f4e5.png)
[MySQL learning notes 25] SQL statement optimization

Aardio - Shadow Gradient Text

【入门】提取不重复的整数

Eigen matrix operation Library

Download xshell and xftp

Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure

Latex formula code

源代码加密的意义和措施

Principle and process of embossing
随机推荐
LSTM of RNN
[getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order
Discussion on several research hotspots of cvpr2022
Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
Deep learning systematic learning
软件测试方法和技术 - 基础知识概括
Significance and measures of source code encryption
5大组合拳,解决校园6大难题,护航教育信息化建设
Gdip - hatchbrush pattern table
使用threejs简单Web3D效果
Erreur de hauteur du clavier souple
Access报表实现小计功能
She is the "HR of others" | ones character
[getting started] intercepting strings
Instead of houses, another kind of capital in China is rising
STM32 uses esp01s to go to the cloud, mqtt FX debugging
LM08丨网格系列之网格反转(精)
[MySQL learning notes 26] view
[force deduction 10 days SQL introduction] Day10 control flow
Vhost kick & call principle