dimanche 22 avril 2018

To print the new string in C++ after eliminating all spaces from the original string input by the user without using any pre-defined function

Below is my full code:

    #include<iostream>
    #include<string.h>
    int main()
    {
        char x[100];
        int i, pos=0, l;
        std::cout<<"Enter a string: ";
        gets(x);
        l=strlen(x);
        for(i=0;i<l;i++)
        {
            if(x[i]==' ')
            pos+=1;
            x[i]=x[i+pos];
            if(x[i]=='\0')
            break;
        }
        std::cout<<"The final string is: "<<x;
        fflush(stdin);
        getchar();
        return 0;
    }

But the output is not coming as expected... Would be very happy if someone pointed out the mistake...

Aucun commentaire:

Enregistrer un commentaire