Q:

Reverse a string in c without using a library function

0

In the interview generally, Interviewer asked the question to reverse a string without using the C library function or maybe they can mention more conditions, it totally depends on the interviewer.

Algorithm:

  • Calculate the length (Len) of string.
  • Initialize the indexes of the array.
    Start = 0, End = Len-1
  • In a loop, swap the value of pszData[Start] with pszData[End].
  • Change the indexes’ of the array as follows.
    Start = start +1; End = end – 1

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
int main(int argc, char *argv[])
{
    char acData[100]= {0}, Temp = 0;
    int iLoop =0, iLen = 0;
    printf("\nEnter the string :");
    gets(acData);
    // calculate length of string
    while(acData[iLen++] != '\0');
    //Remove the null character
    iLen--;
    //Array index start from 0 to (length -1)
    iLen--;
    while (iLoop < iLen)
    {
        Temp = acData[iLoop];
        acData[iLoop] = acData[iLen];
        acData[iLen] = Temp;
        iLoop++;
        iLen--;
    }
    printf("\n\nReverse string is : %s\n\n",acData);
    return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

<< Write your own atoi() in c...