String Reversal Program with Command Line Programming

It is highly advisable to go through Command Line Arguments Post before even looking at the code. Please study this for TCS and come back to this post later.

Here is the code for String Reversal Using Command Line Arguments

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

int main(int argc, char *argv[])
{
   int k;
   char temp;
   int i,j=0;
   int strsize = 0;
   for (i=1; i<argc; i++) {
       strsize += strlen(argv[i]);
        if (argc > i+1)
            strsize++;
   }
   char *cmdstring;
   cmdstring = malloc(strsize);
   cmdstring[0] = '\0';
   for (k=1; k<argc; k++) {
        strcat(cmdstring, argv[k]);
        if (argc > k+1)
            strcat(cmdstring, " ");
   }
   i = 0;
   j = strlen(cmdstring) - 1;
   while (i < j) {
      temp = cmdstring[i];
      cmdstring[i] = cmdstring[j];
      cmdstring[j] = temp;
      i++;
      j--;
   }
   printf("\nReverse string is :%s", cmdstring);
   return(0);

}

Input – abcde

Output – edcba

This code is for String Reversal Program with Command Line Programming with solutions.

Other TCS Coding Questions –