0

In the following part of the string swap code

 end = &str[len - 1];

I am not understanding the addressing part. When I do it without the addressing part it still runs but gives me a warning that "a values of type char cannot be assigned to identity of type char". Here is the full code:

    #include<iostream>
    #include<cstring>
    using namespace std;

int main()
{
    char str[] = "This is a test";
    char *start, *end; 
    int len;
    int t; 

    cout << "Original " << str << "\n";
    len = strlen(str);
    start = str;
    end = str[len - 1];  

//this reverses the string
    while (start < end) { 

        t = *start;  
        *start = *end; 
        *end = t; 

        start++; 
        end--; 

    }
    cout << "Reversed" << str << "\n";
    system("PAUSE");
    return 0;
}
Jeff
  • 3
  • 2
  • *"Reversing string in **C++**"* not like this, please. Not like this. I respect C and C programmers, and what is theirs, shall be given to them. You Have a dedicated type for strings, you have algorithms, you have `swap`... c++ is not only `cout`. Sorry, for complaining, but I feel kinda saddened. – luk32 Jan 09 '17 at 02:22
  • I am working examples through a textbook. – Jeff Jan 09 '17 at 02:38
  • I would red flag it. Seriously. I would consider it harmful. It teaches you obsolete things. You would never have such a problem in a proper c++ program... or is it a C book? This looks like C code with `cout`. That is how C programmers used to teach c++ 15-20 years ago. At least that is my experience. – luk32 Jan 09 '17 at 02:43
  • I am using C++ Beginner's guide second edition by Herbert Schildt. Is there a better book you would recommend for beginners like myself? – Jeff Jan 09 '17 at 03:03
  • I do have one follow up question pls. How come the following does not need to be pointing to an address: start = str; – Jeff Jan 09 '17 at 21:41

3 Answers3

1

I am not understanding the addressing part.

Given

char str[] = "This is a test";
char *start, *end; 
len = strlen(str);

then end is pointer to char, and

end = &str[len - 1]; // `end` points to the last character (before the `\0`)

You must use the & (address of) operator because end is pointer and so it must be assigned to the address of something (here the address of the last character of the string).

When I do it without the addressing part it still runs

I don't think it will - you should have got a compile error

end = str[len - 1]; // invalid conversion from ‘char’ to ‘char*’
artm
  • 17,291
  • 6
  • 38
  • 54
  • I do have one follow up question pls. How come the following does not need to be pointing to an address: start = str; – Jeff Jan 09 '17 at 21:44
  • @Jeff - that's because `str` is a string (not a single character) – artm Jan 09 '17 at 22:40
0

You should konw the type of end is char*, but the type of str[len-1] is char, so you need change type str[n-1] to char*, so you need &str[len-1].

But if you use string, there will be an easy method:

Use the std::reverse method from STL:

std::reverse(str.begin(), str.end()); //str shoud be string type

You will have to include the "algorithm" library, #include.

BlackMamba
  • 10,054
  • 7
  • 44
  • 67
  • Your post doesn't address the OP's question. – R Sahu Jan 09 '17 at 01:51
  • 1
    @RSahu The OP didn't actually ask a question. A reasonable guess is that the question is "what's a sensible way to reverse a string in C++". – David Schwartz Jan 09 '17 at 01:52
  • @DavidSchwartz, they kinda of did: *I am not understanding the addressing part* – R Sahu Jan 09 '17 at 01:53
  • I understand that as the difficulty they ran into trying to solve their actual problem -- how to reverse a string. And the right answer, if that's the question, is not to do it such a horrible way. But this highlights why it's extremely important for someone, after pushing the `Ask Question` button, to actually *ask* a *question*. – David Schwartz Jan 09 '17 at 01:55
  • I appreciate both answers. I understand why you still need it and now understand that there is a better way to accomplish it. I will be more specific next time but the question is answered. Thank you!!! – Jeff Jan 09 '17 at 02:35
  • Would you mind editing this into a full-fledged answer with a complete code example? I believe it would serve OP really nice, to see how a proper c++ solution looks like. – luk32 Jan 09 '17 at 02:45
0

Maybe this can help

void reverse (char word[])
{
   int left = 0;
   int right = strlen(word) - 1;

   while (left < right)
   {
      char temp = word[left];
      word[left] = word[right];
      word[right] = temp;
      left++;
      right--;
   }
   cout << word;

}

I hope this gives you the idea.

Sherwin V
  • 1
  • 1