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
After: 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++ Program to Remove Spaces From String
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++ 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
i am programmer
String After Removing Spaces:
iamprogrammer
iamprogrammer

what of input have more then one space….also.if there is space before n after text.
eg. hello all .