#include <iostream>
usingnamespace std;
char* Reverse(char* s1, int length1)
{
for ( int cnt1 = 0, cnt2 = (length1 - 1); cnt2 > (length1 - 1)/2 ;
cnt1++, cnt2--)
{
/*if( s1[cnt1] == ' ' || s1[cnt2] == ' ' ) //
{ //this is how i
cnt1++; cnt2--; //tried to solve it
}*/ // =>but doesnt
// do anything...
int temp = s1[cnt1];
s1[cnt1] = s1[cnt2];
s1[cnt2] = temp;
}
return s1;
}
int main()
{
char *s = newchar[256];
cout << "Enter a string: ";
cin >> s;
cout << "Reversed string = ";
int length = strlen(s);
Reverse(s, length);
for(int i = 0; i < length; ++i)
cout << s[i];
cout << endl;
delete[] s;
s = 0;
}
string examle: (Problem: in this example, the strings arent reversed)
(here ive just putted something fast together, because i dont have a clue how to do it :))
Okay, I just read your first post completely and understand your second question. The spaces thing is a problem with std::cin. If you need spaces, just don't use std::cin to input. Actually, never use std::cin (except in combination with std::getline(), that is).
Reverse() actually does reverse the string. The problem is that it reverses the wrong string. You're passing a copy of the string. The quickest way to change it is by placing an & after string and before s1 on line 5.
Helios is wrong. The reverse function is reversing the characters in the temporary strings returned by std::string::substr. The s1 string is not being modified at all by the function. Even if it were passed by reference it would make no difference.
1 2 3 4 5 6 7 8 9 10 11
int main()
{
string s = "";
cout << "Enter a string: ";
getline(cin, s);
cout << "Reversed string = ";
std::reverse(s.begin(), s.end());
cout << s << endl;
}
If you still want to code your own reverse function, don't bother with all of the temporary strings in your functions. Read the example code on the website I showed you and do it that way using iterators or simple array positions. You can modify the string in place. You just need a temporary character variable while you are doing each swap or use std::swap. Then again, I don't see the point of rewriting the std::reverse function at all here.
The spaces thing is a problem with std::cin. If you need spaces, just don't use std::cin to input. Actually, never use std::cin (except in combination with std::getline(), that is).
- error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Alloc> &' from 'char *'
for the first one i tried getline(cin, s, ' '); but nothing....
s is this char *s = newchar[256]; , right?
std::getline() takes a reference to an std::string as its second parameter. The third parameter is normally not needed.
What you'll do after getline()ing is allocate a C string with new char[/*name of the string*/.size()+1]
Then simply strcpy(/*name of the allocated C string*/,/*name of the string*/.c_str())
strcpy() is defined in <cstring>.
well first of all, now i realised that you cant read spaces with char, rather, you need a C STRING EQUIVALENT - which makes things a bit different of course
i did what you said, but it still wont read from the white space on
The only things that you will learn by attempting to rewrite the std library are bad habits. For one thing you are still reverting to the use of dynamically allocated character arrays and making extra, unnecessary copies of the input string. You should be using only the std::string that encapulates the original string that was received from the input stream. Moreover, if you really wanted to understand how the reverse function works you'd be attempting to use iterators rather than passing the character array. The reason I say that this is a bad habit is that I have seen numerous instances of professional programmers rewriting these functions out of ignorance because they never took the time to go through the examples and actually learn what all is available within the std library. If you really want to develop good habits, read every single example (on this website) of every single std algorithm and container class and write sample programs that use those algorithms that are provided for you. In fact this website provides some sample programs that are very easy to compile and debug. This would be a much better learning approach then attempting to rewrite those library functions and containers. Most of the rewrites that I have seen attempted on this site are like poor karaoke performances.
you know what, i agree with you completely and ill listen to your advice, but if i make such programs BUT not use them in real programs, just for learning purposes, i think ill be ok...