1. Name this program reverse.c- This program prints all the comm nand-line argum
ID: 3594439 • Letter: 1
Question
1. Name this program reverse.c- This program prints all the comm nand-line arguments backwards (from the last argument to the first, printing each word backwards). Two sample executions are shown below ./a.out hello world dlrow olleh tuo.a/. :/a.out Crimson Tide Alabama UA AU amaba1A ediT nosmirC tuo.a/. Your program must use a function to reverse the arguments. This function takes a single argument (a character string) and modifies that character string. You can write the function signature for this function in either of the ways shown below (since the name of an array is also a pointer to the start of the array). void reverse (char *) void reverse (char 1) Hint-the algorithm in zy Books 5.8 for reversing an array of integers can also be used to reverse a word.Explanation / Answer
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//functuin declaration
void reverse(char *);
//argc is number input from string
//argv is number of arguments
int main(int argc,char *argv[])
{
int i,j,k=0,sum=0;
for(i=1;i<argc;i++)
{
sum = sum+strlen(argv[i]);
}
sum = sum + (argc-1);
char *p = malloc(sum);
for(i = 1; i <argc; i++)
{
for(j = 0;argv[i][j]!='';j++)
{
p[k] = argv[i][j];
k++;
}
p[k++]=' ';
}
k--;
p[k] = '';
printf("Command line input string are : %s",p);
//calling reverse function to reverse string
reverse(p);
}
//reverse function defination
void reverse(char *p)
{
int i,j;
//here we are using pointer replacing character from ending to starting
for(i=0,j=strlen(p)-1; i<j; i++,j--)
{
int t;
t = *(p+i);
*(p+i) = *(p+j);
*(p+j) = t;
}
//string reverse logic completed
printf(" The reverse string = %s ",p);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.