1234567891011121314151617181920212223242526272829303132333435
#include <iostream> using namespace std; void RS (char arr[] , int size) // RS means remove spaces { for (int x=0; arr[x]!='\0'; x++) { if (arr[x]==' ') { arr[x]=arr[x+1]; } x--; } for (int j=0; arr[j]!='\0'; j++) { cout << arr[j]; } cout<<endl; } int main() { char name [100]; cout <<"Enter a sentence"<<endl; cin.getline(name,100); RS (name , 100); }
123456789
for (int x=0; arr[x]!='\0'; x++) { if (arr[x]==' ') { arr[x]=arr[x+1]; } x--; }
for (int x=0; arr[x]!='\0'; x++)// <------+ This plus... { // | if (arr[x]==' ') // | { // | arr[x]=arr[x+1]; // | } // | x--; // <-------------------------+ // ...negates this minus }
123456789101112131415161718192021222324252627282930313233343536
#include <iostream> using namespace std; void RS (char arr[] , int size) { for (int x=0; arr[x]!='\0'; x++) { if (arr[x]==' ') { for (int k =x; arr[k]!='\0'; k++) arr[k]=arr[k+1]; x--; } } for (int j=0; arr[j]!='\0'; j++) { cout << arr[j]; } cout<<endl; } int main() { char name [100]; cout <<"Enter a sentence"<<endl; cin.getline(name,100); RS (name , 100); }