Reputation: 3091
I wrote the following code to reverse the string without using <algorithm> reverse
. I wrote:
#include <string.h>
#include <iostream>
using namespace std;
void reverse(char *str){
int sizeStr=strlen(str);
int firstChar,lastChar,temp;
for(lastChar = (sizeStr - 1),firstChar = 0 ; lastChar>firstChar ; lastChar--, firstChar++){
temp = str[firstChar];
str[firstChar]=str[lastChar];
str[lastChar] = temp;
}
}
int main() {
char *str;
str = "myname";
reverse(str);
cout << str << endl;
return 0;
}
I am getting segfault at str[firstChar]=str[lastChar];
. Please help me figure out my mistake.
NOTE: I declared char str[] = "myname"
and worked perfectly. But as per your all explanations got to learn differences for *str
and str[]
. Thanks a lot. Goes a long way in helping a new programmer.
Upvotes: 1
Views: 3253
Reputation: 22290
You cannot modify a constant string literal.
Use std::string
(thanks @chris for this). Use std::swap
for swapping the characters. (See C++ string swap character places)
malloc
a buffer (with the length of the string), and strcpy
the string to it, and then do the reverse
for the newly allocated buffer.
Upvotes: 5
Reputation: 740
#include <string.h>
#include <iostream>
void myreverse(std::string &s) {
size_t begin = 0, end = s.length();
while ((begin != end) && (begin != --end))
std::swap(s[begin++], s[end]);
}
int main() {
const char *str = "myname";
std::cout << str << std::endl;
std::string newstr(str);
myreverse(newstr);
std::cout << newstr << std::endl;
return 0;
}
Upvotes: 0
Reputation: 967
#include <string.h>
#include <iostream>
using namespace std;
void reverse(char *str){
int sizeStr=strlen(str);
int firstChar,lastChar,temp;
for(lastChar = (sizeStr - 1),firstChar = 0 ; lastChar>firstChar ; lastChar--, firstChar++){
temp = str[firstChar];
str[firstChar]=str[lastChar];
str[lastChar] = temp;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char *str;
str = "myname";
char *str2 = new char [strlen(str)+1];
strcpy(str2,str);
reverse(str2);
cout << str2 << endl;
delete [] str2;
str2 = NULL;
cout << str << endl;
return 0;
}
Upvotes: 0
Reputation: 510
using std::string, you can use the method below to reverse a string
string reverse_string(string arg){
int length = arg.length();
char res[length];
for(int i = length - 1; i >= 0; i--){
res[length - (i+1)] = arg[i];
}
string result(res);
return result;
}
Let me know if you have any further questions.
Upvotes: 1
Reputation: 1045
I think you might be going about this problem the hard way. If you use a std::string the problem becomes much easier. Below is a more generic C++1y solution which will be able to reverse many different types of containers. The container must override the [ ] operator.
#include <iostream>
template <typename T>
auto rev(T &&a){
for (auto i = 0; i < a.size()/2; ++i){
std::swap(a[i], a[a.size()-1-i]);
}
return a;
}
int main(){
std::string x = "hello";
std::cout<< rev(x);
}
Upvotes: 0
Reputation: 5361
The reason is you have not allocated the memory to the character pointer str.
#include <string.h>
#include <iostream>
using namespace std;
void reverse(char *str){
int sizeStr=strlen(str);
int firstChar,lastChar,temp;
for(lastChar = (sizeStr - 1),firstChar = 0 ; lastChar>firstChar ; lastChar--, firstChar++){
temp = str[firstChar];
str[firstChar]=str[lastChar];
str[lastChar] = temp;
}
}
int main() {
char *str;
// Here use malloc to allocate memory to the character pointer str
// Or instead of using a char pointer use a character array to initialize the string
// char *str = malloc(strlen("myname") + 1) or usr char str[10] = "myname"
str = "myname";
reverse(str);
cout << str << endl;
return 0;
}
Upvotes: 0
Reputation: 967
The content of str is constant, you should use char pointer instead.
Upvotes: 0