Write a C program that will take a number of command line arguments. It will be
ID: 3732007 • Letter: W
Question
Write a C program that will take a number of command line arguments. It will be able to receive any combination of strings and integers. Assume there will be no more than 100 arguments sent to the program.
This program should:
1. Sum up all integers it receives and output total
2. Each string should be checked to see if it's a substring of any future string and will output each time this occurs.
3. Program must have at least one function other than main. This function should be in its own source file and have its own .h header file. You will also need a source file for the main body, so your main.c, func.h and func.c
4. Include a makefile
Example:
$ make
1 2 3 4 one hund onehund onehundred 5 6 7
will output :
Sum of integers: 28
one is a substring of onehund
one is a substring of onehundred
hund is a substring of onehund
hund is a substring of onehundred
onehund is a substring of onehundred
Explanation / Answer
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
int main(int c, char *argv[]) {
char arr[100][100];
int sum=0;
int ci=0;
for(int i=0; i<c ; i++) {
int val,k=0;
//printf("%d",atoi(argv[i]));
val = atoi(argv[i]);
if (val == 0){
ci++;
strncpy(arr[k],argv[i], 100);
//arr[k]=sprintf("%s",argv[i]);
k++;
} else {
sum += val;
}
}
printf("The sum is %d", sum);
//printf("%l", sizeof(arr)/sizeof(arr[0]));
for(int j=0;j< ci;j++){
for(int l=j;l<ci;l++){
if(strstr(arr[l],arr[j])!=NULL){
printf("%s is substring of %s ", arr[j],arr[l]);
}
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.