在C中使用putchar和getchar删除多个空格
问题:编写一个程序,使用在C中使用putchar和getchar删除多个空格,c,string,getchar,putchar,C,String,Getchar,Putchar,问题:编写一个程序,使用getchar()接收文本输入并输出字符串,删除了多个空格 下面是我如何编写伪代码的: While each input character is received before reaching EOF, do the following: 1) if character is non-blank, print it out 2) otherwise: a. print out the blank b. do no
getchar()
接收文本输入并输出字符串,删除了多个空格
下面是我如何编写伪代码的:
While each input character is received before reaching EOF, do the following:
1) if character is non-blank, print it out
2) otherwise:
a. print out the blank
b. do nothing untill the next non-blank character
3) if a non-blank character is reached, go back to 1)
我试图实现如下算法:
#include <stdio.h>
/* replaces multiple blanks with a single blank */
main(){
char c;
while((c= getchar())!=EOF){
if (c != ' ')
putchar(c);
else {
putchar(c);
while(c == ' ')
;
}
}
}
#包括
/*用一个空白替换多个空白*/
main(){
字符c;
而((c=getchar())!=EOF){
如果(c!='')
普查尔(c);
否则{
普查尔(c);
while(c==“”)
;
}
}
}
当字符串包含空格时,它将暂停。我不知道该如何调试它。我认为问题在于我的第二个在时,程序进入无限循环,而不是等待新字符 #包括
#include <stdio.h>
/* replaces multiple blanks with a single blank */
main(){
int c; // thanx chux
while((c= getchar())!=EOF){
if (c != ' ')
putchar(c);
else {
putchar(c);
while((c= getchar())!=EOF)
if (c!=' ')
{
putchar(c);
break;
}
}
}
}
/*用一个空白替换多个空白*/
main(){
int c;//thanx chux
而((c=getchar())!=EOF){
如果(c!='')
普查尔(c);
否则{
普查尔(c);
而((c=getchar())!=EOF)
如果(c!='')
{
普查尔(c);
打破
}
}
}
}
上一个while没有从stdin读取字符,导致infinie循环比较上一个getchar()的最后一个红色字符。\include
内部主(空){
INTC;
而((c=getchar())!=EOF){
如果(c!='')
普查尔(c);
否则{
普查尔(c);
而((c=getchar())=“”)
;
ungetc(c,stdin);//返回1
}
}
返回0;
}
Anonymous的答案是可行的,但有一种更简单的算法也能起作用:
While there is input remaining:
Read a character.
If the current and previous characters aren't both blank:
Print the current character.
在C中:
#包括
int main(){
int prev=EOF,c;
而((c=getchar())!=EOF){
如果(c!=”| | prev!=”)
普查尔(c);
prev=c;
}
返回0;
}
这对我很有效
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c;
int space = 0;
while ((c = getchar())!= EOF){
if (c != ' '){
putchar(c);
}else{
if(space == ' '){
continue;
}else{
putchar(c);
}
}
space = c;
}
return 0;
}
#包括
#包括
int main()
{
INTC;
int空间=0;
而((c=getchar())!=EOF){
如果(c!=''){
普查尔(c);
}否则{
如果(空格=“”){
继续;
}否则{
普查尔(c);
}
}
空间=c;
}
返回0;
}
您的程序存在一些问题:
main()
的原型必须包含返回类型int
c
必须定义为int
,以便您可以正确区分EOF
与getchar()
返回的所有有效字节值
- 识别空白字符后,必须继续读取字符并跳过后续空白
- 从技术上讲,空白字符包括空格字符
'
和制表符'\t'
。您应该使用
中的isblank()
,并修改程序以跳过后续的空白字符
以下是修改后的版本:
#include <ctype.h>
#include <stdio.h>
/* replaces multiple blanks with a single blank */
int main(void) {
int c;
while ((c = getchar()) != EOF) {
putchar(c);
if (isblank(c)) {
while (isblank(c = getchar())
continue;
if (c == EOF)
break;
putchar(c);
}
}
return 0;
}
#包括
#包括
/*用一个空白替换多个空白*/
内部主(空){
INTC;
而((c=getchar())!=EOF){
普查尔(c);
如果(i为空(c)){
while(isblank(c=getchar())
继续;
如果(c==EOF)
打破
普查尔(c);
}
}
返回0;
}
while(c=='');
是无限循环。更新c
.NMDV,但使用int c
,因此EOF
与所有256个不同的char
main()
。还要添加一个返回0;
以获得良好的风格。这并不是我想要的。我给了它输入我在这里学习。
并且我得到了我正在学习。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c;
int space = 0;
while ((c = getchar())!= EOF){
if (c != ' '){
putchar(c);
}else{
if(space == ' '){
continue;
}else{
putchar(c);
}
}
space = c;
}
return 0;
}
#include <ctype.h>
#include <stdio.h>
/* replaces multiple blanks with a single blank */
int main(void) {
int c;
while ((c = getchar()) != EOF) {
putchar(c);
if (isblank(c)) {
while (isblank(c = getchar())
continue;
if (c == EOF)
break;
putchar(c);
}
}
return 0;
}