Skip to content

Instantly share code, notes, and snippets.

@StephanieMak
Forked from ThunderXu/CareerCup1.2.cpp
Last active August 29, 2015 14:27
Show Gist options
  • Save StephanieMak/473a3c7dccc80d4fd321 to your computer and use it in GitHub Desktop.
Save StephanieMak/473a3c7dccc80d4fd321 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--;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment