-1

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...

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
DeBARtha
  • 460
  • 6
  • 17
  • Recommend adding what you expect to see. All we can do is infer from what the code does, and it is, by your admission, wrong. – user4581301 Apr 22 '18 at 16:49
  • I see a lot of "pre-defined functions" in there, e.g. `strlen`. – Paul R Apr 22 '18 at 16:49
  • Also `fflush(stdin)` results in UB, and `gets` is deprecated. – Paul R Apr 22 '18 at 16:50
  • `gets` is a function so broken that it has been removed from C and deprecated in C++. Read that as :"Do Not Use." – user4581301 Apr 22 '18 at 16:50
  • 1
    Can we talk you into using `std::string` and `std::cin`? – user4581301 Apr 22 '18 at 16:52
  • What's the expected output and what are you getting? – 3vts Apr 22 '18 at 16:53
  • Suppose the user enters:"My name is John." I want to get:"MynameisJohn." – DeBARtha Apr 22 '18 at 16:56
  • I am getting:"Myname s Jhn." – DeBARtha Apr 22 '18 at 17:02
  • 1
    `gets` is removed from `c++` and is definitely not in `c++11` – Killzone Kid Apr 22 '18 at 17:25
  • 1
    @DebarthaPaul -- *Would be very happy if someone pointed out the mistake...* -- [I downvoted because...](http://idownvotedbecau.se/nodebugging/). Debugging your own code is part and parcel of learning how to write programs. Just writing a program, seeing that it doesn't produce the correct results, and then asking someone else (stackoverflow members in this case) to debug your code is not how it's supposed to work. – PaulMcKenzie Apr 22 '18 at 17:28
  • 1
    @DebarthaPaul Do you want to simply print out the string with spaces, or actually change the string so the spaces are removed? Printing the string without spaces is much different than changing the string. – PaulMcKenzie Apr 22 '18 at 18:09
  • 1
    You're supposed to be learning C++, but your code is pretty much mostly C. I would recommend getting a [good book](https://stackoverflow.com/q/388242/9254539), which would have taught you how to use C++ input functions, and `std::string` instead of torturing you with C input and char arrays. – eesiraed Apr 22 '18 at 19:08

4 Answers4

0

Final check for break should be:

if(X[i+pos] == '\0')
    break;
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
code707
  • 1,663
  • 1
  • 8
  • 20
0

The issue is that you keep checking the place you want to copy to for ' ', not the place you want to copy from.

So it should be while(x[i+pos]==' ') instead of if(x[i]==' ') (while instead of if to be able to handle multiple spaces in a row)

You can easily find these kind of errors by stepping through your code with a debugger and checking the values of all relevant variables to see if they match your expectations.

PeterT
  • 7,981
  • 1
  • 26
  • 34
0

I would simplify your code to something like this:

#include<iostream>

int main()
{
    char x[100];
    std::cout<<"Enter a string: ";
    gets(x);
    std::cout << "The final string is: ";
    for(int i = 0; i < 100; i++)
    {
        if(x[i] == '\0') break;
        while(x[i] == ' ') i++;
        std::cout << x[i];
    }
    getchar();
    return 0;
}
3vts
  • 778
  • 1
  • 12
  • 25
0

If your goal is to change the string, then the strategy could be

1) Find the first space character. If no space character was found, we can end processing. If a space is found, then this is the first position to change and the first position to check (these are distinct positions, even though they start out at the same place).

2) Process the string using these two indices, one pointing to the position to check, and the other pointing to the position to change.

3) If the position to check is not a space, copy it to the position to change and increment the position to change.

4) Increment the position to check. Go to step 3) until the end of string is encountered (position to check is at the end-of-string).

5) Null terminate the string at the position to change. Done.

Here is an implementation of the above:

#include <iostream>
#include <cstring>

int main()
{
    char str[] = "Hello, my name is John";
    size_t len = strlen(str);
    size_t change_position = 0;

    // find the first space character
    for (change_position = 0; change_position < len; ++change_position)
    {
        if (str[change_position] == ' ')
            break;  // found it
    }  

    if (change_position != len)  // if we found a space character
    {
        // starting from the first space
        for (size_t check_position = change_position; check_position < len; ++check_position)
        {
            // if the check position is not a space 
            if (str[check_position] != ' ')
            {
               // copy it to the change position
                str[change_position] = str[check_position];
               // update change position  
                ++change_position;
            }
        }
    }

    // finish up by null-terminating the string
    str[change_position] = '\0';
    std::cout << str;
}

Live Example

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • My answer helps others who will search for this question, and thus find a solution. Your code should have used the strategy employed in the answer given. Your attempt was too far off, so I also gave you an explanation of how to solve the problem. – PaulMcKenzie Apr 24 '18 at 15:13