Home / Programs / String Reversal Program with Command Line Programming
🚀 Programming Example

String Reversal Program with Command Line Programming

👁 472 Views
💻 Practical Program
📘 Step Learning
String Reversal Program with Command Line Programming

💻 Program Code

// Program by atnyla
// String Reversal Program with Command Line Programming

#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);

}
                        

🖥 Program Output

Smile


Reverse string is :elimS


                            

📘 Explanation

Nope
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.