CareerCup-1.2

Write code to reverse a C-Style String (C-String means that “abcd” is represented as five characters, including the null character )

#include <iostream>
using namespace std;

void swap2(char* a, char* b)
{ 
    char temp = *a;
    *a = *b;
    *b = temp;
};

void reverse(char *str)
{
    if(str == NULL)return;
    int len = strlen(str);
    char *end = str+len-1;
    while(str < end)
    {
        swap2(str++, end--);
    }
}; 

int main()
{
    char str[] = "abcdefg";
    reverse(str); 
    cout<<str;
    system("pause"); 
};

Comments

comments powered by Disqus