问题描述
我正在编写此程序(适用于k& r-2nd版本的练习1-9)
删除字符串中的额外空格
example-" test string"将是测试字符串
#include< stdio.h>
#include< string.h>
#包括< conio.h>
main()
{
char str [20],st [20];
int i = 0,j = 0;
printf(" enter string");
得到(str);
while(str [i]!= NULL)
{
if(str [i] =='''')
{st [j ++] = st [i ++];
while(str [i] =='''')i ++;
}
其他
st [j ++] = str [i ++];
}
st [j ] = NULL;
strcpy(str,st);
puts(str);
}
(用turbo c ++编译为windows 3.1版)
i知道应该有其他方法来编写这个程序(这是一个糟糕的
程序),有人可以帮我吗以紧凑的方式编写该程序
并给我一些逻辑和提示。
i想要使用gcc但我没有linux操作系统和一个
软件允许gcc在windows中编译(cygwin)不起作用。
任何人都可以建议我这样的程序,允许在
windows中使用gcc进行编译(这是关于主题但是建议总是欢迎)。
thankx
i am writing this program (for exercise1-9 in k&r-2nd edition) which
removes extra spaces in string
example- "test string" will be "test string"
#include<stdio.h>
#include<string.h>
#include<conio.h>
main()
{
char str[20],st[20];
int i=0,j=0;
printf("enter string");
gets(str);
while(str[i]!=NULL)
{
if(str[i]=='' '')
{ st[j++]=st[i++];
while(str[i]=='' '') i++;
}
else
st[j++]=str[i++];
}
st[j]=NULL;
strcpy(str,st);
puts(str);
}
(compiled in turbo c++ for windows version 3.1)
i know there should be other ways to write this program (thats a bad
program ),can someone help me in writing that program in compact way
and give me some logic and tips.
i want to use gcc but i don`t have linux operating system and one
software which allows gcc compiling in windows (cygwin) is not working.
anyone can advice me such a program that allows compiling with gcc in
windows( this is off topic but advices are always welcome).
thankx
推荐答案
分开你正在做的功能。
在这种情况下你想要
/ *
将空格跨越单个空格
(和修剪)领先/尾随空格?)
* /
void onespace(char * out,char * in)
决定是否你想允许和允许相同。这将使你的
功能
更方便使用,但你可能需要更加小心
编码。
基本上你维护两个指针,一个写指针。指针和读取指针指针。
如果你点击了
你写入写的非空格。指针和递增两者。
如果你碰到一个空格字符,你就可以在写
指针上写一个空格。然后你增加阅读指针,虽然它仍然指向
到空格。
-
购买我的书12普通无神论者论点(反驳)
Separate out ewhat you are doing into a function.
In this case you want
/*
turns whitespace spans into single spaces
(and trims leading / trailing white space?)
*/
void onespace(char *out, char *in)
Decide if you want to allow out and in to be the same. This will make your
function
more convenient to use, but you might have to be a little bit more careful
in coding it.
Basically you maintain two pointer, a "write" pointer and a "read" pointer.
If you hit
a non-whitespace you write to the "write" pointer and increment both.
If you hit a whitespace character, you write a single space to the "write"
pointer. Then you increment the "read" pointer whilst it is still pointing
to white space.
--
Buy my book 12 Common Atheist Arguments (refuted)
这篇关于一点点错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!