Write a program to accept a string from the keyboard, reverse the string, capita
ID: 3883660 • Letter: W
Question
Write a program to accept a string from the keyboard, reverse the string, capitalize al alphabet chars in the string, print each char on its own line, print the number of alphabet chars capitalized and print the length of the string. If the prompt and input string is: Enter a string: Hello World! The output should be: ! D L R O W O L L E H The string is 12 chars and 8 chars were capitalized. You should write one function for each operation and call them from main. The string and the integer for length should be declared in main and passed to called functions. You should try sketching a rough algorithm and test it on paper before coding. You can NOT use C library functions to get the length of a string, convert to upper case and test for lower case. You should write you own functions for these operations. Main function - calls other functions Function to get the length of a string Function to test if a char is lower case Function to convert a lower case char to uppercase Function to get string from keyboard. Function to reverse string. Function to capitalize all alpha chars and count number capitalized. Function to print output.Explanation / Answer
#include <stdio.h>
#include <string.h>
int upper_string(char s[]) {
int c = 0;
int x=0;
while (s[c] != '') {
if (s[c] >= 'a' && s[c] <= 'z') {
s[c] = s[c] - 32;
x++;
}
c++;
}
return x;
}
int main()
{
char s[100], r[100];
int n, c, d;
int i=0,x;
printf("Input a string ");
gets(s);
n = strlen(s);
for (c = n - 1, d = 0; c >= 0; c--, d++)
r[d] = s[c];
r[d] = '';
x=upper_string(r);
while(r[i]!=''){
printf("%c ", r[i]);
i++;
}
printf("The string is %d char and %d char were capitalized",n,x);
return 0;
}
Output:
Input a string Hello world!
!
D
L
R
O
W
O
L
L
E
H
The string is 12 char and 10 char were capitalized
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.