For example given string is “the crazy programmer”. Now we have to remove all the spaces present in the string.
例如,给定的字符串是“疯狂的程序员”。 现在,我们必须删除字符串中存在的所有空格。
Before: the crazy programmer
之前:疯狂的程序员
After: thecrazyprogrammer
之后: thecrazyprogrammer
Below I have shared a program that perform above task with simple approach. You can ask your queries in the comment section.
下面,我分享了一个使用简单方法执行上述任务的程序。 您可以在评论部分提出您的查询。
C / C ++程序从字符串中删除空格 (C/C++ Program to Remove Spaces From String)
C程序 (C Program)
#include<stdio.h>
int main()
{
int i,j=0;
char str[30];
printf("Enter a String:\n");
gets(str);
for(i=0;str[i]!='\0';++i)
{
if(str[i]!=' ')
str[j++]=str[i];
}
str[j]='\0';
printf("\nString After Removing Spaces:\n%s",str);
return 0;
}
C ++程序 (C++ Program)
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
int i,j=0;
char str[30];
cout<<"Enter a String:\n";
gets(str);
for(i=0;str[i]!='\0';++i)
{
if(str[i]!=' ')
str[j++]=str[i];
}
str[j]='\0';
cout<<"\nString After Removing Spaces:\n"<<str;
return 0;
}
Output
输出量
Enter a String:
输入一个字符串:
i am programmer
我是程序员
String After Removing Spaces:
空格后的字符串:
iamprogrammer
iamprogrammer
翻译自: https://www.thecrazyprogrammer.com/2015/06/cc-program-to-remove-spaces-from-string.html