0

I'm trying to reverse a string using an array and string. This is my code. I am facing an issue when I try to print reverse_s for any string, e.g. "raj". So it is giving me "jar", i.e. the reverse of string, but the main problem is it is giving me some random characters after "jar" just like jar▀≈¶@. Please help me finding the problem and debug my code.

#include<bits/stdc++.h>
using namespace std;

int main(){
    string s;
    cin>>s;
    int lenOfStr = s.length();
    cout<<lenOfStr<<endl;
    char reverse_s[lenOfStr];
    int j=0;
    for(int i=(lenOfStr-1); i>=0; i--){
        reverse_s[j] = s[i];
        j++;
        if(j==(lenOfStr)){
            break;
        }
    }
    cout<<"Reverse string is "<<reverse_s<<endl;
    // sizeof operator can be used to find out the lenght of array in cpp

    cout<<"Thelength of my array is "<<sizeof(reverse_s)<<endl;
    cout<<reverse_s[5]<<endl; 
    return 0;
}

I'm new in programming.

JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • `#include` is not standard C++ header, `using namespace std;` is bad practice, `char reverse_s[lenOfStr];` is not standard C++ feature, C strings are null-terminated char arrays. – 273K Mar 11 '23 at 16:37
  • Does this answer your question? [C++ copying chars from static to dynamic array adds a bunch of random elements](https://stackoverflow.com/questions/29690675/c-copying-chars-from-static-to-dynamic-array-adds-a-bunch-of-random-elements) – JaMiT Mar 11 '23 at 20:18
  • If this were not a class assignment on `for` loop practice, I'd `#include ` and: `std::string rev; rev.reserve(length(s));rev.assign(crbegin(s), crend(s));` ranges could be used too, but that can wait. – Red.Wave Mar 12 '23 at 08:49

2 Answers2

2

C Strings need to be null terminated.

So allocate one extra byte of space in your array, and set it to '\0'.

char reverse_s[lenOfStr+1];
reverse_s[lenOfStr] = '\0';
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
-1

i think you are confusing char arrays with strings, the former is a simple array with no upstream checking, string on the other hand is a complex construct with some attributes and methods. Here it is clearly explained: Difference between string and char[] types in C++

and about your main question, the random values are caused by missing endline character ('\0') in the reverse_s array, so when you print the array, it keeps reading memory until it finds a zero value. this is my solution, based on yours, using strings directly, it's not the best but it works:

#include<bits/stdc++.h>
using namespace std;

int main(){
    string s, reverse_s;
    cin>>s; // ! cin does not read strings with spaces
    for(int i=s.length();i>=0;i--){
        reverse_s.push_back(s[i]); // i use the string reverse_s like a stack, it's not an appropriate use, but it works
    }
    cout<<"Reverse string is "<<reverse_s<<endl;
 }

to best answer your question I suggest you take a look at this tutorial: https://www.digitalocean.com/community/tutorials/reverse-string-c-plus-plus

MrLakige
  • 1
  • 3
  • 1
    *"i think you are confusing char arrays with strings"* -- I don't get this impression from the question. The question uses both a character array intended to hold a C-style (null-terminated) string and a `std::string`. I see no attempt to use `std::string` methods on the character array. – JaMiT Mar 11 '23 at 20:21
  • edit: I corrected my answer because I forgot to include the part: "and about your main question, the random values are caused by the missing endline character ('\0') in the reverse_s array, so when you print the array, it keeps reading memory until it finds a zero value." – MrLakige Mar 11 '23 at 20:51
  • 1
    *"endline character ('\0')"* -- the null character is not an "endline character". The line-ending character is the newline character, `\n`, although in some contexts the carriage return character, `\r`, marks the end of a line (and sometimes both characters are used). So correct code, but wrong name for the character. – JaMiT Mar 12 '23 at 02:25