error

Pretty much what im trying to do is reverse an entered string. I haven't used C++ in a couple years (I've been working with Java) and im trying to get back into it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>

using namespace std;
int main()
{
  string word;
  cin >> word;
  cout << reverseString(word);
  return 0;
}

string reverseString(string String)
{
  string newString;
  for (int i = 0; i < String.length(); i++)
  {
    if (String[i] == "a")
    {
      newString += "z";
    }
    else if (String[i] == "b")
    {
      newString += "y";
    }
    else if (String[i] == "c")
    {
      newString += "x";
    }
  }
  return newString;
}

1
2
3
4
5
6
7
8
9
10
11
12
1>c:\users\X\documents\visual studio 2010\projects\parse string\parse string\main.cpp(9): error C3861: 'reverseString': identifier not found
1>c:\users\X\documents\visual studio 2010\projects\parse string\parse string\main.cpp(16): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\X\documents\visual studio 2010\projects\parse string\parse string\main.cpp(18): error C2446: '==' : no conversion from 'const char *' to 'int'
1>          There is no context in which this conversion is possible
1>c:\users\X\documents\visual studio 2010\projects\parse string\parse string\main.cpp(18): error C2040: '==' : 'int' differs in levels of indirection from 'const char [2]'
1>c:\users\X\documents\visual studio 2010\projects\parse string\parse string\main.cpp(22): error C2446: '==' : no conversion from 'const char *' to 'int'
1>          There is no context in which this conversion is possible
1>c:\users\X\documents\visual studio 2010\projects\parse string\parse string\main.cpp(22): error C2040: '==' : 'int' differs in levels of indirection from 'const char [2]'
1>c:\users\X\documents\visual studio 2010\projects\parse string\parse string\main.cpp(26): error C2446: '==' : no conversion from 'const char *' to 'int'
1>          There is no context in which this conversion is possible
1>c:\users\X\documents\visual studio 2010\projects\parse string\parse string\main.cpp(26): error C2040: '==' : 'int' differs in levels of indirection from 'const char [2]'
1>
"a" is a string literal, you meant 'a', which is a character literal.
reverseString must be declared before its first use in main().

And before you type that out for the entire alphabet:
1
2
3
4
5
6
string reverseString(const string& str)
{
  string ns=str;
  for (uint i=0;i<ns.length();i++)if (ns[i]>='a' && ns[i]<='z')ns[i]='z'-(ns[i]-'a');
  return ns;
}
Last edited on
or recursion, elegant, and only few lines.
Yeah, but if you put it a decently long string you might overflow the stack.
Topic archived. No new replies allowed.