|
CS256 - Fundamentals of Structured Programming
| Displaying exercises/e7/solution/p1.c
#include <stdio.h>
#include <stdlib.h>
/**
* Create a program with the following usage:
* Usage: p1 <number> <string>
* which prints the <string>, <number> of times with a leading numeric prefix
* (starting at 1) padded to three spaces with a space after it.
* If not enough parameters are provided, print the above usage and exit.
* Use the atoi() function to convert the first parameter to a real integer
* number.
* Example:
* > ./p1 3 Hello
* 1 Hello
* 2 Hello
* 3 Hello
*/
int main(int argc, char *argv[])
{
if (argc < 3) {
printf("Usage: p1 <number> <string>\n");
return 0;
}
int num = atoi(argv[1]);
for(int i=0; i < num; i++) {
printf("%3d %s\n", i+1, argv[2]);
}
return 0;
}
|