-1

I have recently started learning C++ and have written a few lines which accepts a string, displays the number of characters in it and also displays the reverse of the input string. This is what I have written.

#include <iostream>

int main()

{
  char string[25],rev_string[25];
  int counter=0,length=0;

  std::cout << "\n Enter the string : ";
  std::cin >> string;

  while(counter==!'\0')
  {
    counter=counter+1;
    length=length+1;
  }
  counter=0;

  std::cout << "The string has "<<length<<" characters in it.";
  while(length>=0)
  {
    rev_string[counter]=string[length];
    counter=counter+1;
    length=length-1;
  }

  std::cout << "\n The reverse of the given string is : "<<rev_string;

  return(0);
}

There is no error when I debug, however when I run the program, I get some unexpected value and the string length shows zero. Can you please point out where have I made mistakes.

This is what I get when I run the program.

jww
  • 97,681
  • 90
  • 411
  • 885
  • 3
    `==!'\0'` is not what you want. Try `!='\0'`. And perhaps something other than counter? – AJNeufeld Oct 21 '18 at 03:44
  • 2
    In c++ you should use `std::string` instead of `char[]` – eyllanesc Oct 21 '18 at 03:44
  • Also see [`std::reverse`](https://en.cppreference.com/w/cpp/algorithm/reverse). If you can't use `std::reverse` then I would expect to see a `std::swap` somewhere. And of course you have [How do you reverse a string in place in C or C++?](https://stackoverflow.com/q/198199/608639), [How to reverse an std::string?](https://stackoverflow.com/q/4951796/608639) and friends. – jww Oct 21 '18 at 03:45
  • And without std::reverse, you will want to start with a pointer to the start and end character and then swap `while (end > start)` (don't swap over the entire string or you will end up reversing the string twice and ending up right back where you started) – David C. Rankin Oct 21 '18 at 03:50

2 Answers2

1
while ( counter == !'\0' ) { … }

Well, !'\0' is true, which as an integer is 1. So you have while (counter == 1), and counter is initialized to 0, making the expression immediately false, so the loop never executes.

You probably meant to write != '\0'. But this is still a problem, since counter starts off with the value 0, and 0 != 0 is still false and the loop doesn’t loop.

When you input Hey as your string, the characters H, e, y, and \0 are placed in the string variable. You want to find where that \0 character is, which we see is at string[3]. So why are you comparing counter with '\0'? Maybe you want string[counter]?


When you get the number of characters in Hey, which is 3, you begin your reverse loop copying the \0 at index 3 to index 0 ... all 4 characters in the reverse order: \0, y, e, H. Unfortunately, the \0 at the start will mark the end of the string, so the string will appear empty..

AJNeufeld
  • 8,526
  • 1
  • 25
  • 44
  • I somehow got the program to show how many characters are their in it by fixing `while(counter==!'\0')` by changing it to `while(string[counter]!='\0')` . But I still cant get the program to display the reverse string. It just shows blank now. –  Oct 21 '18 at 04:14
  • I added a line `length=length-1;` to prevent it from copying the `\0`. The program displays the reverse string this time, but it is followed by some random characters which are different every time I run the program. –  Oct 21 '18 at 04:24
  • Maybe you still need to copy the `\0` somewhere? – AJNeufeld Oct 21 '18 at 04:25
  • Added `rev_string[counter]='\0';` after the loop and now it works perfectly fine. Thanks! –  Oct 21 '18 at 04:39
1

C++ is difficult to learn if you learn it as C. And also you get some ugly habits. Please learn and practice C++.

#include <string>
#include <algorithm>
#include <iostream>

int main()
{
    std::string s;
    std::cin >> s;

    std::cout << "number of characters: " << s.size() << std::endl;

    std::reverse(s.begin(), s.end());
    std::cout << "reverse string: " << s << std::endl;
}

This is how you write your problem in C++. Now compare with what you are trying to do and I think it speaks for itself.

bolov
  • 72,283
  • 15
  • 145
  • 224