-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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