C Programming /* This program will be called with one command line argument that
ID: 3794630 • Letter: C
Question
C Programming
/*
This program will be called with one command line argument that
contains a string followed by an asterisk and an integer. Print
out the string as many time as indicated by the integer. For
example, when called as prog Hi*3, you print HiHiHi.
Hint: Look for the '*' starting from the back of the string.
len = strlen(arg) gives you the string length.
When you have found the '*', then arg + len + 1 is a pointer
to the integer that you can pass to atoi as in the preceding
exercise. Replace the '*' with a '' so you can print the
part that precedes it.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
int count = 1;
char* arg;
int len;
if (argc < 2) return -1;
arg = argv[1];
len = strlen(arg) - 1;
...
for (int i = 0; i < count; i++)
printf("%s", arg);
printf(" ");
return 0;
}
Explanation / Answer
This tis the proper working code.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
int main(int argc, char* argv[])
{
int number, count = 0;
char* arg;
int len;
if (argc < 2)
return -1;
arg = argv[1];
int stringLength = strlen(arg);
len = stringLength-1;
while(arg[len]!='*')
len--;
printf("%d", len);
number = atoi(arg + len+1);
printf(" %d", number);
arg[len] = '';
for (int i = 0; i < number; i++)
printf("%s", arg);
printf(" ");
getch();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.