0

I've received a task, where I have to change every 5th character of a character array to = character and then shift the other elements of the array right, like this: I like fruit --> I lik=e fru=it.

I have trouble with how to shift the elements after the = characters. I was considering something similar to simple sorting, but I just can't imagine how to switch and which elements. Could you help me out, please? Thanks in advance!

I could complete the code this far:

#include<cstring>
#include<fstream>

int main(){
    
    char sentence[] = "I like fruit";
    int length = strlen(sentence);
    
    for(int i = 1; i < length; i++){
        if(i % 5 == 0){
            sentence[i] = '=';
        }
    }
    
    std::cout << sentence << '\n';
    
    return 0;
}
silverfox
  • 1,568
  • 10
  • 27
Ms Cookie
  • 3
  • 4
  • 3
    `sentence` is of fixed size, you cannot add more characters to it. Is there a reason you are not using `std::string` ? – 463035818_is_not_an_ai Jun 07 '21 at 08:12
  • Do you have to do it in-place? – MarkSouls Jun 07 '21 at 08:12
  • @463035818_is_not_a_number it has to be a char array, sadly I can't use string :( I just left it there by accident, at first I wanted to do with that but then saw the point in my task. Let me correct it. Also, I was thinking about a dynamic char array, would it be possible to do the shift using that? – Ms Cookie Jun 07 '21 at 08:17
  • Considering `e frui` is 6 characters, shouldn't the output be something like `I lik=e fru=it`? – silverfox Jun 07 '21 at 08:17
  • @MarkSouls what do you mean by in-place? ^^' if you mean I can use a function to make the shift, I can – Ms Cookie Jun 07 '21 at 08:18
  • @silverfox ohh yes, that's the correct one, sorry ^^' corrected it – Ms Cookie Jun 07 '21 at 08:20
  • In place means to operate within original memory, just modifying ```sentence``` in this case. If it doesn't have to be in place, you can make another ```char[]``` and use it as result array. – MarkSouls Jun 07 '21 at 08:22
  • `std::string` is a dynamic char array (with some bells and whistles added) – 463035818_is_not_an_ai Jun 07 '21 at 08:22
  • @MarkSouls it is not possible to add characters to the string "in-place", there is not enough place for them – 463035818_is_not_an_ai Jun 07 '21 at 08:23
  • 1
    Please include the full and complete assignment or exercise text, so we can know all possible requirements and limitations. – Some programmer dude Jun 07 '21 at 08:24
  • @463035818_is_not_a_number Well, we don't really know if the OP is allowed to start with a big enough array or, worse, use `malloc/realloc`. – Bob__ Jun 07 '21 at 08:29
  • @Bob__ if we take only what is written in the question then the array is not big enough. The task is "I have to change every 5th character of a character array". Nevermind, the question maybe needs a little clarification. If they cannot use `std::string`, what can they use instead? – 463035818_is_not_an_ai Jun 07 '21 at 08:31

1 Answers1

2

From running your code, it can be seen that the characters would be replaced, not inserted.

Your code result:

Input : I like fruit

Output : I lik= fru=t

A viable way is to input a std::string, use substr() to split the input into segments of 5, then adding a = afterward.

The problem with using char[] is that it's of fixed size, meaning nothing can be added/removed from it (although you can still modified it normally). std::string is dynamic (in general it is a dynamic char array with a few differences), so it could change its size.

#include<string>
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    string inp; cout << "Input : "; getline(cin, inp);
    int len = inp.length(); string res = "";

    for (int i = 0; i < len; i+=5)
    {
        string tmp = inp.substr(i, 5); //splitting the string
        if (tmp.size() == 5) {tmp += '=';} //adding '='
        res += tmp;
    }
    cout << res;
}

Output:

Input : I like fruit
I lik=e fru=it

More info : Difference between std::string and char[] in C++.

silverfox
  • 1,568
  • 10
  • 27