当前位置:网站首页>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 !!!!!!

边栏推荐
- Aardio - Method of self constructed geticonhandle
- [getting started] extract non repeating integers
- 【Redis】一气呵成,带你了解Redis安装与连接
- 谈谈数字化转型的几个关键问题
- Serial port oscilloscope software ns-scope
- Latex table
- Gui Gui programming (XV) - use scale to control font size changes
- [MySQL learning notes 28] storage function
- Uni hot update
- [website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions
猜你喜欢

Aardio - 阴影渐变文字

What information does the supplier need to know about Audi EDI project?

How to make the two financial transactions faster

web254

【批处理DOS-CMD-汇总】扩展变量-延迟变量cmd /v:on、cmd /v:off、setlocal enabledelayedexpansion、DisableDelayedExpansion

How to troubleshoot SharePoint online map network drive failure?
![[getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order](/img/87/07783593dbabcf29700fa207ecda08.png)
[getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order

Connect timed out of database connection

P4 安装bmv2 详细教程
![[getting started] extract non repeating integers](/img/88/3e96df88e980bd98ac112b18a8678c.png)
[getting started] extract non repeating integers
随机推荐
Deep learning systematic learning
数字转excel的字符串坐标
Cmake I two ways to compile source files
【力扣10天SQL入门】Day9 控制流
AArdio - 【问题】bass库回调时内存增长的问题
php laravel微信支付
量化交易之读书篇 - 《征服市场的人》读书笔记
[getting started] input n integers and output the smallest K of them
Transaction method call @transactional
How do the top ten securities firms open accounts? In addition, is it safe to open a mobile account?
Array: question brushing record
Scala语言学习-07-构造器
Li Kou daily question - day 31 -1502 Judge whether an arithmetic sequence can be formed
EDA open source simulation tool verilator beginner 6: debugging examples
谈谈数字化转型的几个关键问题
Differential: definition of total differential, partial derivative, gradient
P4 安装bmv2 详细教程
How to troubleshoot SharePoint online map network drive failure?
[kv260] generate chip temperature curve with xadc
PWN攻防世界int_overflow