How to Reverse the String in C with Example Code

#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "string.h"
#include "stdlib.h"

int _tmain(int argc, _TCHAR* argv[])
    char pstr[] = "My Name is Kapil Thakar";
    int len = strlen(pstr);
    printf("Original String: %s\n", pstr);
    printf("String Length: %d\n",len);

    printf("Last Char: %c\n", *(pstr+(len-1)));

    for(int i=0; i<((len-1)/2); i++)
        int temp;
        temp = *(pstr+(len-1-i));     
        *(pstr+(len-1-i))= *(pstr+i);
        *(pstr+i) = temp;
    printf("Modified Stage 1 String: %s\n", pstr);

    int spoint = 0;
    int epoint = 0;
    int j, k ,temp;;
    for(j=0; j<=((len)); j++)
        printf("\nCurrent Pos:%c\n",*(pstr+j));
        if( (*(pstr+j) == ' ') || (*(pstr+j) == NULL) ){
            epoint = j;
            printf("Start Pos:%d\n",spoint);
            printf("End Pos:%d\n",epoint);
            int hlen = ((epoint-spoint)/2);
            for(k=0; k<(hlen); k++)
                temp = *(pstr+(epoint-1-k));     
                *(pstr+(epoint-1-k))= *(pstr+spoint+k);
                *(pstr+spoint+k) = temp;     
                printf("Loop: %s\n", pstr);
            spoint = epoint+1;

    printf("Modified Stage 2 String: %s\n", pstr);

    return 0;


Original String: My Name is Kapil Thakar
String Length: 23
Last Char: r
Modified Stage 1 String: rakahT lipaK si emaN yM
Current Pos:r
Current Pos:a
Current Pos:k
Current Pos:a
Current Pos:h
Current Pos:T
Current Pos:
Start Pos:0
End Pos:6
Loop: Takahr lipaK si emaN yM
Loop: Thkaar lipaK si emaN yM
Loop: Thakar lipaK si emaN yM
Current Pos:l
Current Pos:i
Current Pos:p
Current Pos:a
Current Pos:K
Current Pos:
Start Pos:7
End Pos:12
Loop: Thakar Kipal si emaN yM
Loop: Thakar Kapil si emaN yM
Current Pos:s
Current Pos:i
Current Pos:
Start Pos:13
End Pos:15
Loop: Thakar Kapil is emaN yM
Current Pos:e
Current Pos:m
Current Pos:a
Current Pos:N
Current Pos:
Start Pos:16
End Pos:20
Loop: Thakar Kapil is Nmae yM
Loop: Thakar Kapil is Name yM
Current Pos:y
Current Pos:M
Current Pos:
Start Pos:21
End Pos:23
Loop: Thakar Kapil is Name My
Modified Stage 2 String: Thakar Kapil is Name My

Related Post:

Removing Space from Given String & Different Methods to Count Number of Ones in Given Value

Booting Sequence for Boot Process in Linux

How to count number of 1 or set bits in a given number

What is Segmentation fault ?

How to Access Private Data Members in C++ without using friend function

Process Vs Thread 

How to Add Two Numbers Without Using + Operator in C 

Overview of Function Pointer in C with Example - What is function pointer & how to use it

How to Fork child process with waitpid & execl in linux with c example code

Share on Google Plus

About Unknown

"I am Kapil Thakar, an Embedded Engineer cum Blogger wants to learn new things. I love to share my knowledge solutions to the problems. Interested in Blogging, Creative-Writing, SEO, Website Creation, Video Making, Editing, Affiliation Programs, Online Making Money."
    Blogger Comment
    Facebook Comment


Post a Comment

Related Posts Plugin for WordPress, Blogger...