从字符串中的特定大小写中删除 Space
Remove a Space from a Specific case in a string
我做了一个简单的程序,可以从字符串中删除所有 space,但我想要的是一个从 字符串的开头删除 space 的程序 如果有另一个程序从 字符串的末尾删除 space
希望这是有道理的
这是我的 c 程序,它从所有给出
的字符串中删除 spaces
#include<stdio.h>
int main()
{
int i,j=0;
char str[50];
printf("Donnez une chaine: ");
gets(str);
for(i=0;str[i]!='[=11=]';++i)
{
if(str[i]!=' ')
str[j++]=str[i];
}
str[j]='[=11=]';
printf("\nSans Espace: %s",str);
return 0;
}
下方接近
- 首先删除前导白色字符。
- 然后移动字符串。
然后删除尾随的白色字符。
char *beg = str;
char *travel = str;
/*After while loop travel will point to non whitespace char*/
while(*travel == ' ') travel++;
/*Removes the leading white spaces by shifting chars*/
while(*beg = *travel){
beg++;
travel++;
}
/* travel will be pointing to [=10=] char*/
if(travel != str) {
travel--;
beg--;
}
/*Remove the trailing white chars*/
while(*travel == ' ' && travel != beg) travel--;
/*Mark the end of the string*/
if(travel != str) *(travel+1) = '[=10=]';
我做了一个简单的程序,可以从字符串中删除所有 space,但我想要的是一个从 字符串的开头删除 space 的程序 如果有另一个程序从 字符串的末尾删除 space
希望这是有道理的
这是我的 c 程序,它从所有给出
的字符串中删除 spaces#include<stdio.h>
int main()
{
int i,j=0;
char str[50];
printf("Donnez une chaine: ");
gets(str);
for(i=0;str[i]!='[=11=]';++i)
{
if(str[i]!=' ')
str[j++]=str[i];
}
str[j]='[=11=]';
printf("\nSans Espace: %s",str);
return 0;
}
下方接近
- 首先删除前导白色字符。
- 然后移动字符串。
然后删除尾随的白色字符。
char *beg = str; char *travel = str; /*After while loop travel will point to non whitespace char*/ while(*travel == ' ') travel++; /*Removes the leading white spaces by shifting chars*/ while(*beg = *travel){ beg++; travel++; } /* travel will be pointing to [=10=] char*/ if(travel != str) { travel--; beg--; } /*Remove the trailing white chars*/ while(*travel == ' ' && travel != beg) travel--; /*Mark the end of the string*/ if(travel != str) *(travel+1) = '[=10=]';