Tcs Command Line Program for String Reversal
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
[code language=”cpp”]
#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); }
[/code]
Input – abcde
Output – edcba
This code is for String Reversal Program with Command Line Programming with solutions.
Other TCS Coding Questions –
- TCS Coding Questions – 1
- TCS Coding Questions – 2
- Area of a Triangle
- TCS Command Line Arguments – Fibonacci Series
- TCS Command Line Program to Swap two numbers
- TCS String Reversal Using Command Line Programming
- Greatest of Two Numbers using CLP
- LCM of Two Number using CLP
- Average of Two Numbers
- Sum of Digits of a number
- Binary to Decimal
- Decimal to Binary
- Factorial of a Number
- Square Root of Prime Number
- Armstrong Number
Login/Signup to comment
m trying to reverse a string using command line argument but I don’t know why am I getting segmentation fault for this .
#include
#include
#include
int main(int argc, char *argv[]) {
char *str = argv[1];
char *rev;
int i, j, k;
for (i = 0; str[i] != ‘\0’; i++); {
k = i – 1;
}
for (j = 0; j <= i – 1; j++) {
rev[j] = str[k];
k–;
}
printf("The reverse string is %s\n", rev);
return 0;
}
#include
#include
int main(int argc, char *argv[])
{
char temp;
char *str;
int i,j;
str=argv[1];
i=0;
j=strlen(str)-1;
while(i<j)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
i++;
j–;
}
printf("Reversed string is: %s",str);
return 0;
}
more easy way
// cpp program for string reversal
#include
using namespace std;
int main(int argc, char *argv[])
{
string s=argv[1];
reverse(s.begin(),s.end());
cout<<s;
return 0;
}
main(int argc, char *argv[]){
char temp;
char *string
int i, j;
if(argc!=2){
printf(“Invalid Usage.\n”);
printf(“Usage Example: ./a.out string_to_reverse”);
return 1;
}
str=argv[1];
i=0;
j=strlen(str)-1
while(i<j){
temp=str[i];
str[i]=str[j];
str[j]=temp;
i++;
j–;
}
print(str);
return 0;
}
more simple this
command line