当前位置:网站首页>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 !!!!!!
边栏推荐
- sqlalchemy创建MySQL_Table
- 力扣——求一组字符中的第一个回文字符
- Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
- [staff] high and low octave mark (the notes in the high octave mark | mark range are increased by one octave as a whole | low octave mark | mark range are decreased by one octave as a whole)
- Gdip - hatchbrush pattern table
- [kv260] generate chip temperature curve with xadc
- go通用动态重试机制解决方案的实现与封装
- Kickback -- find the first palindrome character in a group of characters
- Instead of houses, another kind of capital in China is rising
- PostgreSQL source code learning (26) -- windows vscode remote debugging PostgreSQL on Linux
猜你喜欢
[kv260] generate chip temperature curve with xadc
Teach you how to apply for domestic trademark online step by step
【入门】输入整型数组和排序标识,对其元素按照升序或降序进行排序
STM32 uses esp01s to go to the cloud, mqtt FX debugging
如何使用layui将数据库中的数据以表格的形式展现出来
PWN attack and defense world int_ overflow
【mysql学习笔记25】sql语句优化
[getting started] intercepting strings
Why some people earn nearly 10billion a year, while others earn 3000 a month: the details you ignore actually make the most money
The Windows C disk is full
随机推荐
Php laraver Wechat payment
Differential: definition of total differential, partial derivative, gradient
getInputStream() has already been called for this request
Two expressions of string
getInputStream() has already been called for this request
[MySQL learning notes 28] storage function
Introduction to kubernetes resource objects and common commands (II)
Programmer's regimen
empirical study and case study
Caesar
Download xshell and xftp
[untitled]
How to check ad user information?
如何使用layui将数据库中的数据以表格的形式展现出来
Contenttype comparison of all types
Access报表实现小计功能
The database is locked. Is there a solution
PostgreSQL source code learning (26) -- windows vscode remote debugging PostgreSQL on Linux
Li Kou daily question - day 31 -1790 Can a string exchange be performed only once to make two strings equal
Connect timed out of database connection