UIU: Learn C by Examples : Reverse


Reverse string naive approach


#include <cstdio>
#include <cstring>

using namespace std;

int main(int argc, const char * argv[])
{
    char mainString [50] = "Hello";

    printf ("Main String: %s\n", mainString);

    int lengthMainString = strlen(mainString);

    char reverseString [50];

    int reverseIndex = lengthMainString - 1;

    for ( int i = 0; i < lengthMainString; i++ ) {
        reverseString [reverseIndex] = mainString [i];
        reverseIndex--;
    }

    reverseString [lengthMainString] = '\0';

    printf ("Reverse String: %s\n", reverseString);

    return 0;
}


Main String: Hello
Reverse String: olleH

Reverse string (length / 2) approach


#include <cstdio>
#include <cstring>

using namespace std;

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

int main(int argc, const char * argv[])
{
    char mainString [50] = "Hello";

    printf ("Main String: %s\n", mainString);

    int lengthMainString = strlen(mainString);

    for ( int i = 0; i < lengthMainString / 2; i++ ) {
        swap(&mainString [i], &mainString [lengthMainString - 1 - i]);
    }

    printf ("Reverse String: %s\n", mainString);

    return 0;
}


Main String: Hello
Reverse String: olleH

Reverse string STL approach


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int main(int argc, const char * argv[])
{
    char mainString [50] = "Hello";

    printf ("Main String: %s\n", mainString);

    int lengthMainString = strlen(mainString);

    reverse(mainString, mainString + lengthMainString);

    printf ("Reverse String: %s\n", mainString);

    return 0;
}



Main String: Hello
Reverse String: olleH

Reverse number sprintf approach


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>

using namespace std;

int main(int argc, const char * argv[])
{
    int n = 12345;

    char number [50];

    sprintf(number, "%d", n);

    reverse(number, number + strlen (number));

    printf ("Reverse Number: %s\n", number);

    // convert a string to integer
    int reverseNumber = atoi(number);

    printf ("Reverse Number: %d\n", reverseNumber);

    return 0;
}


Reverse Number: 54321
Reverse Number: 54321

Reverse number modulus approach


#include <cstdio>

using namespace std;

int reverseNumber(int n)
{
    int ret = 0;

    while ( n ) {
        ret = 10 * ret + n % 10;
        n /= 10;
    }

    return ret;
}

int main(int argc, const char * argv[])
{
    int n = 12345;

    printf ("Reverse Number: %d\n", reverseNumber(n));

    return 0;
}


Reverse Number: 54321

Advertisements

One thought on “UIU: Learn C by Examples : Reverse

  1. #include
    #include

    int main(){

    char mainString[50] = “hello”, temp;

    int stringLength = strlen(mainString),i = 0, j;

    j = stringLength-1;

    while(i < j){

    temp = mainString[i];
    mainString[i] = mainString[j];
    mainString[j] = temp;

    i++;
    j–;

    }

    mainString[stringLength] = '\0';

    printf("%s", mainString);

    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s