当前位置:网站首页>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?
![[untitled]](/img/c2/63286ba00321c9cdef43ff40635a67.png)
[untitled]
![[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)](/img/48/de19e8cc007b93a027a906d4d423b2.png)
[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)

Gdip - hatchBrush图案表

Soft keyboard height error

Microsoft stream - how to modify video subtitles
![[kv260] generate chip temperature curve with xadc](/img/fc/e5e4648b09b1123b2d494b75a9f8f7.png)
[kv260] generate chip temperature curve with xadc

Erreur de hauteur du clavier souple

SQL number injection and character injection
随机推荐
力扣每日一题-第31天-1502.判断能否形成等差数列
Connect timed out of database connection
Anddroid 文本合成语音TTS实现
Basic knowledge of MATLAB
【mysql学习笔记27】存储过程
Aardio - [problem] the problem of memory growth during the callback of bass Library
Office365 - how to use stream app to watch offline files at any time
[getting started] extract non repeating integers
Gdip - hatchBrush图案表
IMDB practice of emotion classification (simplernn, LSTM, Gru)
图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
Download xshell and xftp
[question brushing] character statistics [0]
Latex table
【无标题】
Instead of houses, another kind of capital in China is rising
【Redis】一气呵成,带你了解Redis安装与连接
【mysql学习笔记26】视图
[force deduction 10 days SQL introduction] Day10 control flow
OJ输入输出练习