c - 从字符串中的特定大小写中删除空格
问题描述
我做了一个简单的程序,从字符串中删除所有空格,但我想要一个程序从字符串的开头删除空格(如果有的话)和另一个程序从字符串的末尾删除空格
希望这是有道理的
这是我的 c 程序,它从所有字符串中删除空格
#include<stdio.h>
int main()
{
int i,j=0;
char str[50];
printf("Donnez une chaine: ");
gets(str);
for(i=0;str[i]!='\0';++i)
{
if(str[i]!=' ')
str[j++]=str[i];
}
str[j]='\0';
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 \0 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) = '\0';
推荐阅读
- python - Null 不是 Dash Plotly 中的对象(评估“n.layout”)
- firebase - 如何在简单的flutter-dart代码中使用对象snapshot.data实现空安全?
- powershell - 有没有办法在没有 Windows 的情况下生成 Microsoft 序列化证书存储?
- javascript - 在 Javascript 中集成 Eventlistner 不起作用
- python - f"{tesseract_cmd} 未安装或不在您的 PATH 中。"
- python - 查找程序的路径(python)
- swift - RealityKit ARkit:从 raycast 中找到锚点(或实体) - 始终为零
- python - sklearn/statsmodels 奇异协方差矩阵下普通最小二乘法的结果
- android - MPAndroidChart 绘制不正确的线性图
- wicket - 如果所有包含的 Wicket 组件都不可见,则隐藏 HTML 元素