Write a C program that looks at command-line arguments and does an analysis for
ID: 673801 • Letter: W
Question
Write a C program that looks at command-line arguments and does an analysis for each of them.
For this task, we define a string to be growing if each character in the string is greater than, or equal to, the character before it. Recall that all characters have an actual integer value asociated with them. The character ’a’ has a decimal equivalent of 97, ’b’ has a decimal equivalent of 98, and so on.
The string ace is considered to be growing as ’a’ is less than ’c’ is less than ’e.’ On the other hand, the string tide is not growing (’i’ is less than ’t’ and ’d’ is less than ’i’).
If you want, take a look at http://asciitable.com. This site has a clean ASCII table in case you need a referesher on how ASCII works. How much a string has grown is the sum of all the differences between characters. For example, ace grew by four (4), as ’c’ is two greater than ’a’ and ’e’ is two greater than ’c.’ The strong app grew by fifteen (15), as the first ’p’ is fifteen greater than ’a’ (112 - 97) and there was no growth between the first ’p’ and the second ’p’ in the string (since they are the same character). Your program should examine all the command line arguments and print out those strings that are considered to be growing. In addition, identify the string that grew the most. For this program, your main routine should have a loop that processes all command line arguments. Do not process argv[0], the name of the executable itself. Within this loop:
• You need a function to compute whether or not a string grew. It should return a 1 (one, true) if the string grew and a 0 (zero, false) if the string did not grow.
• You need a second function to determine how much a string grew. This function only gets called if you encounter a string that did actually grow. Calling this function with the string ace would return 4 (four), as c is two greater than a and e is two greater than c.
Your program should run like this:
Important - if you can not get the second function to work, that displays how much each growing string actually grew, then at least be sure to print the names of growing strings (the results from the first function).
Explanation / Answer
#include<stdio.h>
#include<string.h>
int main()
{
char string [100];
int c =0, count[26] ={0};
printf("enter a string");
gets(string);
while(string[c] != '')
{
if (string[c] >='a' && string [c] <='z')
count[string[c]-'a'++;
c++;
}
for (c=0; c<26;c++)
{
if(count[c] ! =0)
printf("%c"occur %d times enetr the string ",c+'a',count[c];
}
retrun 0;
}
void grew(char s[],int count[]){
int c =0;
while (s[c] ! =''){
if (s[c] >='a' && s[c] <='z')
count[s[c]-'a']++;
c++;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.