删除多余的空白 [英] Remove extra blanks
问题描述
嗨专家,
我正在尝试编写一个程序,用一个空格替换一个字符串中的两个或多个连续的
空格。
这就是我的所作所为:
#include< stdio.h>
#include< string。 h>
#define MAX 80
int main()
{
char s [ MAX];
int i,j;
fgets(s,MAX,stdin);
i = strlen(s);
while(i)
{
while(s [ - i]!=''''&& i> 0)/ *找到最后一个空格* /
;
j = i;
while(s [ - j] ==''''& & j> 0)/ *转到最后一个非空格* /
; / * char之前的s [i] * /
if(s [j]!='''')
j ++; / *增加j使得s [j]是一个空格* /
if(j< i)/ *如果找到了额外的空格,删除* /
while(s [i])/ *他们左移* /
s [++ j] = s [++ i]; / *右边的字符* /
i = j;
}
puts(s);
返回0;
}
该程序运行正常,但我觉得我已经做到了
不必要的复杂。
有谁可以建议我可以改进代码的方法?有没有更优秀的算法?
注册用户写道:
>
我正在尝试编写一个程序,用一个空格替换一个字符串中的两个或多个连续的
空格。
这就是我的所作所为:
#include< stdio.h>
#include < string.h>
#define MAX 80
int main()
{
char s [MAX];
int i,j;
fgets(s,MAX,stdin);
i = strlen(s) ;
while(i)
{
while(s [ - i]!=''''&& i> 0)/ *找到最后一个空格* /
;
j = i;
while(s [ - j] =='' ''&& j> 0)/ *转到最后一个非空格* /
; / * char之前的s [i] * /
if(s [j]!='''')
j ++; / *增加j使得s [j]是一个空格* /
if(j< i)/ *如果找到了额外的空格,删除* /
while(s [i])/ *他们左移* /
s [++ j] = s [++ i]; / *右边的字符* /
i = j;
}
puts(s);
返回0;
}
该程序运行正常,但我觉得我已经做到了< b / b
不必要的复杂。
有谁可以建议我可以改进代码的方法?
是一个更好的算法吗?
试试这个。注意没有字符串缓冲区。应该工作到
你会得到超过32767个连续空白。
#include< stdio.h>
int main(void )
{
int blanks,ch;
blanks = 0;
while(EOF) !=(ch = getchar())){
if('''= = ch){
++ blanks;
if(1 == blanks)putchar('''');
}
else {
putchar(ch);
blanks = 0;
}
}
返回0;
}
-
Chuck F(cinefalconer at maineline dot net)
可用于咨询/临时嵌入式和系统。
< http://cbfalconer.home.att.net>
"注册用户" < in ******************* @ gmail.comwrites:
嗨专家,
我正在尝试编写一个程序,用一个空格替换一个字符串中的两个或多个连续的
空格。
这里'我的所作所为:
#include< stdio.h>
#include< string.h>
#define MAX 80
int main()
{
char s [MAX];
int i,j;
fgets(s,MAX,stdin);
i = strlen(s);
while(i)
{
while(s [ - i]!=''''&& i> 0)/ *查找最后一个空格* /
;
j = i;
while(s [ - j] ==''''&& j> 0)/ *转到最后一个非空格* /
; / * char之前的s [i] * /
if(s [j]!='''')
j ++; / *增加j使得s [j]是一个空格* /
if(j< i)/ *如果找到了额外的空格,删除* /
while(s [i])/ *他们左移* /
s [++ j] = s [++ i]; / *右边的字符* /
i = j;
}
puts(s);
返回0;
}
该程序运行正常,但我觉得我已经做到了
不必要的复杂。
有谁可以建议我可以改进代码的方法?有没有
更好的算法?
更快更快高效的IMO(在大多数情况下我会认为)将
malloc换成新的字符串,将原来的字符串复制到其中,然后更新
原件 - 不重复洗牌。
strcpy(refCopy,refStr)
char * d = refStr; / *目的地* /
char * s = refCopy; / *原始字符串副本 - 源* /
而(* d ++ =(ch = * s ++))
if(ch ==''''){
while((ch = * s ++)&&(ch =='''')); / *吞噬下列空格
if(!(* d ++ = ch))/ *存储第一个非空格* /
休息;
}
未经测试,但您会明白这一点。
-
-
CBFalconer写道:
注册用户写道:
> I我正在尝试编写一个程序,用一个空格替换一个字符串中的两个或多个连续的空格。
这就是我所做的:
> #include< stdio.h>
#include< string.h>
#define MAX 80
int main()
{
char s [MAX];
int i,j;
fgets(s,MAX,stdin);
i = strlen(s);
while(i)
{
while(s [ - i]!=''''&& i> 0)/ *找到最后一个空格* /
;
j = i;
while(s [ - j] ==''''&&am p; j> 0)/ *转到最后一个非空格* /
; / * char之前的s [i] * /
if(s [j]!='''')
j ++; / *增加j以使s [j]为空格* /
如果(j while(s [i ])/ *他们左移* /
s [++ j] = s [++ i]; / *右边的字符* /
i = j;
}
put(s);
返回0;
这个程序运行正常,但我觉得我已经把它变得不必要了。
任何人都可以建议我可以改进的方法在代码?有更好的算法吗?
试试吧。注意没有字符串缓冲区。应该工作到
你会得到超过32767个连续空白。
#include< stdio.h>
int main(void )
{
int blanks,ch;
blanks = 0;
while(EOF) !=(ch = getchar())){
if('''= = ch){
++ blanks;
if(1 == blanks)putchar('''');
}
else {
putchar(ch);
blanks = 0;
}
}
返回0;
}
或者..
#include< stdio.h>
int main(void) {
int ch,last = 0;
while((ch = getchar())!= EOF){
if(!( ch ==''''&& last == ch))
putchar(ch);
last = ch;
}
返回0;
}
-
Joe Wright
一切都应该是m尽可能简单,但并不简单。
---阿尔伯特爱因斯坦---
Hi experts,
I''m trying to write a program that replaces two or more consecutive
blanks in a string by a single blank.
Here''s what I did:
#include <stdio.h>
#include <string.h>
#define MAX 80
int main()
{
char s[MAX];
int i, j;
fgets(s, MAX, stdin);
i=strlen(s);
while(i)
{
while (s[--i]!='' '' && i>0) /*Find the last space*/
;
j=i;
while (s[--j]=='' '' && j>0) /*Go to the last non-space*/
; /*char before s[i]*/
if (s[j]!='' '')
j++; /*Increment j so that s[j] is a space*/
if (j<i) /*If extra spaces have been found, remove*/
while (s[i]) /*them by left shifting */
s[++j]=s[++i]; /*the characters on the right*/
i=j;
}
puts(s);
return 0;
}
The program works fine, but I have a feeling that I''ve made it
unnecessarily complicated.
Can anyone suggest ways on which I can improve upon the code? Is there
a better algorithm?
Registered User wrote:>
I''m trying to write a program that replaces two or more consecutive
blanks in a string by a single blank.
Here''s what I did:
#include <stdio.h>
#include <string.h>
#define MAX 80
int main()
{
char s[MAX];
int i, j;
fgets(s, MAX, stdin);
i=strlen(s);
while(i)
{
while (s[--i]!='' '' && i>0) /*Find the last space*/
;
j=i;
while (s[--j]=='' '' && j>0) /*Go to the last non-space*/
; /*char before s[i]*/
if (s[j]!='' '')
j++; /*Increment j so that s[j] is a space*/
if (j<i) /*If extra spaces have been found, remove*/
while (s[i]) /*them by left shifting */
s[++j]=s[++i]; /*the characters on the right*/
i=j;
}
puts(s);
return 0;
}
The program works fine, but I have a feeling that I''ve made it
unnecessarily complicated.
Can anyone suggest ways on which I can improve upon the code? Is there
a better algorithm?Try this. Notice the absence of string buffers. Should work until
you get over 32767 consecutive blanks.
#include <stdio.h>
int main(void)
{
int blanks, ch;
blanks = 0;
while (EOF != (ch = getchar())) {
if ('' '' == ch) {
++blanks;
if (1 == blanks) putchar('' '');
}
else {
putchar(ch);
blanks = 0;
}
}
return 0;
}
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
"Registered User" <in*******************@gmail.comwrites:
Hi experts,
I''m trying to write a program that replaces two or more consecutive
blanks in a string by a single blank.
Here''s what I did:
#include <stdio.h>
#include <string.h>
#define MAX 80
int main()
{
char s[MAX];
int i, j;
fgets(s, MAX, stdin);
i=strlen(s);
while(i)
{
while (s[--i]!='' '' && i>0) /*Find the last space*/
;
j=i;
while (s[--j]=='' '' && j>0) /*Go to the last non-space*/
; /*char before s[i]*/
if (s[j]!='' '')
j++; /*Increment j so that s[j] is a space*/
if (j<i) /*If extra spaces have been found, remove*/
while (s[i]) /*them by left shifting */
s[++j]=s[++i]; /*the characters on the right*/
i=j;
}
puts(s);
return 0;
}
The program works fine, but I have a feeling that I''ve made it
unnecessarily complicated.
Can anyone suggest ways on which I can improve upon the code? Is there
a better algorithm?
Much faster & efficient IMO (in most cases I would think) to
malloc a new string, copy the original one into it and then update the
original in place - no repeated shuffling.
strcpy(refCopy,refStr)
char *d=refStr; /*destination*/
char *s=refCopy; /*original string copy - source*/
while(*d++=(ch=*s++))
if(ch=='' ''){
while((ch=*s++)&&(ch=='' '')); /*gobble up following spaces
if (!(*d++=ch)) /* store first non space */
break;
}
Not tested, but you will get the idea.
--
--
CBFalconer wrote:Registered User wrote:>I''m trying to write a program that replaces two or more consecutive
blanks in a string by a single blank.
Here''s what I did:
#include <stdio.h>
#include <string.h>
#define MAX 80
int main()
{
char s[MAX];
int i, j;
fgets(s, MAX, stdin);
i=strlen(s);
while(i)
{
while (s[--i]!='' '' && i>0) /*Find the last space*/
;
j=i;
while (s[--j]=='' '' && j>0) /*Go to the last non-space*/
; /*char before s[i]*/
if (s[j]!='' '')
j++; /*Increment j so that s[j] is a space*/
if (j<i) /*If extra spaces have been found, remove*/
while (s[i]) /*them by left shifting */
s[++j]=s[++i]; /*the characters on the right*/
i=j;
}
puts(s);
return 0;
}
The program works fine, but I have a feeling that I''ve made it
unnecessarily complicated.
Can anyone suggest ways on which I can improve upon the code? Is there
a better algorithm?
Try this. Notice the absence of string buffers. Should work until
you get over 32767 consecutive blanks.
#include <stdio.h>
int main(void)
{
int blanks, ch;
blanks = 0;
while (EOF != (ch = getchar())) {
if ('' '' == ch) {
++blanks;
if (1 == blanks) putchar('' '');
}
else {
putchar(ch);
blanks = 0;
}
}
return 0;
}
Or maybe..
#include <stdio.h>
int main(void) {
int ch, last = 0;
while ((ch = getchar()) != EOF) {
if (!(ch == '' '' && last == ch))
putchar(ch);
last = ch;
}
return 0;
}
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
这篇关于删除多余的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!