Write a C++ Program to delete spaces from a sentence, where sentence is entered by the user.

Program Code:
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
char str[80];
int i=0, len, j;

cout<<"Enter the sentence to delete space in it is: ";
gets(str);
cout<<endl;
len=strlen(str);
for(i=0; i<len; i++)
{
if(str[i]==' ')
{
for(j=i; j<len; j++)
{
str[j]=str[j+1];
}
len--;
}
}
cout<<"String after removing spaces in it is : ";
cout<<str;
return 0;
}

The program output is tested on www.jdoodle.com
Output:

Enter the sentence to delete space in it is: Mukesh Rajput Mukesh
String after removing spaces in it is : MukeshRajputMukesh


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: