Header Ads

Embedded C Interview Questions with Answers | Part 6

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);
            printf("hlen:%d\n",hlen);
            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);
    getch();

    return 0;
}


O/P:


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
hlen:3
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
hlen:2
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
hlen:1
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
hlen:2
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
hlen:1
Loop: Thakar Kapil is Name My
Modified Stage 2 String: Thakar Kapil is Name My



How to Concat Two String without Using strcat library function


You can take Two String using either:


  char *str1 = "Kapil";
  char *str2 = "Thakar";

or

  char str1[] = "Kapil";
  char str2[] = "Thakar";

Now to concat these two string you need to create another string or you can use the same string. Here I have taken a new string to hold the combined string.

We don't the size of the new concat string, Concat string should have string length bigger than a sum of the string of string 1 & string 2. So we can either take

char str3[20]; //Here 20 is bigger than sum of string 1 & string 2.
or
char *str3 = (char *) malloc (length);
where length is sum of both string 1 & string 2.

Here you can not use
char str3[length] as you need to know the array size at compile time. So If you do like that then it will give you an error.

If we don't do this, then it will result in the segmentation fault. So It is required.

We can now copy the each character from string 1 & then we start copying text from string 2.
At the end, we need to add the null character to terminate the string properly. Else garbage character will be printed on the screen till it will find the null character.


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


int main()
{

    char *str1 = "Kapil";
    char *str2 = "Thakar";
    int length = (strlen(str1)+strlen(str2));
    char *str3 = (char *) malloc (length);
    int i = 0;
    for(i=0; i<length; i++)
    {
        if(i < strlen(str1))
            *(str3+i) = *(str1+i);
        else   
            *(str3+i) = *(str2+ (i - strlen(str1)));
    }   
    *(str3+i) = '\0';

    printf("\n%d,%s", length,str3);

    getch();
    return 0;
   
}

For any Query please feel free to comment.