-1

My logic is as follows :

#include<bits/stdc++.h>
    using namespace std;
    
    int main(){
        char a[50] = {'h','i',' ','m','e',' ','t','e'};
        
        
        // k = 4 because i have 2 spaces and for each
       // space i have to insert 2 spaces . so total 4 
       //spaces
        int k=4;
        for(int i=strlen(a)-1 ; i>0 && k >0 ; i--){
            if(a[i] != ' ')
            {
                a[i+k] = a[i];
                a[i] = ' ';
            }
            else
            {
                k = k - 2;
            }
            
        }
    
        printf("%s" , a);
    
        return 0;
    }

I have to character array to solve it. I am able to do it using string stl The output i get is hi---me. But the answer is hi---me---te.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
harsh vardhan
  • 25
  • 1
  • 6
  • One problem is that you didn't null-terminate your resulting string properly, so calling `printf` on that may behave weirdly. Step through your algorithm with pencil and paper. You didn't set all of the array elements correctly. – eesiraed Jul 13 '19 at 05:01
  • 2
    Unrelated: `#include` [loads the gun](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). `using namespace std;` [takes the safety off](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Be really careful. – user4581301 Jul 13 '19 at 05:02
  • Another good strategy is to explain the purpose of each line in your program to a [Rubber Duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging). Rubber ducks are notoriously stupid, so you have to explain very carefully. Sometimes through the conversation, the Rubber ducks can lead you to ask interesting questions like what happens to the data at `a[i+k]` when you execute `a[i+k] = a[i];` – user4581301 Jul 13 '19 at 05:11

1 Answers1

1

Your code is tagged C++. But there is nothing C++ in your code. It is pure C.

And, your are including #include<bits/stdc++.h> and using the std namespace using namespace std;. From now on: Please never ever in your whole life do such things again. Or, stop working with C++.

Additionally never ever use plain C-style array like your char a[50] in C++.

In your code you have some bugs. Most critical is the missing terminating 0 and then calling strlen. Before you use a function, always check somewhere, how this function works. Use meaningful variable names. Write comments. Always check boundaries.

I updated your C-Code:

#include <stdio.h>

int main()
{
    // Character String to work on
    char charString[50] = "hi me te";

    // Check all possible positions in string
    for (int index = 0; (index < 49) && (0 != charString[index]); ++index)
    {
        // If there is a space in the string
        if (' ' == charString[index])       
        {
            // Shift all characters one position to the right
            for (int shiftPosition = 48; shiftPosition >= index; --shiftPosition)
            {
                charString[shiftPosition + 1] = charString[shiftPosition];
            }
            ++index;
        }
    }
    // Show result
    printf("%s\n", charString);

    return 0;
}

And here the C++ solution

#include <iostream>
#include <string>
#include <regex>

int main()
{
    // Text to work on
    std::string text("hi me te");

    // Replace every space with 2 spaces. Print result
    std::cout << std::regex_replace(text, std::regex(" "), "  ");

    return 0;
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
A M
  • 14,694
  • 5
  • 19
  • 44