Skip to content

Instantly share code, notes, and snippets.

@ThunderXu
Created February 23, 2013 01:35
Show Gist options
  • Save ThunderXu/5017888 to your computer and use it in GitHub Desktop.
Save ThunderXu/5017888 to your computer and use it in GitHub Desktop.
Implement a function void reverse(char* str) in C or C++ which reverses a null-terminated string.
#include "stdafx.h"
#include <string>
#include <iostream>
void reverse(char*);
int main()
{
using namespace std;
char str[] = "This is a test";
int size = strlen(str);
reverse(str);
cout<<str<<endl;
return 0;
}
void reverse(char* str)
{
char *start = str;
char *end = start+strlen(str)-1;
while(start<end)
{
char temp = *end;
*end = *start;
*start=temp;
start++;
end--;
}
}
@AntonioK180
Copy link

Ty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment