Write a program in C to print a string in reverse using a character pointer. Also print the uppercase of the first letter of the reversed word. Sample Input word Sample Output drow D
Question
Write a program in C to print a string in reverse using a character pointer.
Also print the uppercase of the first letter of the reversed word.
Sample Input
word
Sample Output
drow
D
Solution 1
Sure, here is a simple C program that reverses a string using a character pointer and also prints the uppercase of the first letter of the reversed word.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void reverse(char *str) {
char *end = str;
char temp;
// Find end of the string
while(*end) {
++end;
}
--end;
// Swap characters
while(str < end) {
temp = *str;
*str = *end;
*end = temp;
str++;
end--;
}
}
int main() {
char word[] = "word";
// Reverse the string
reverse(word);
printf("%s\n", word);
// Print the uppercase of the first letter
printf("%c\n", toupper(word[0]));
return 0;
}
This program first reverses the string by swapping the characters from the start and end of the string. Then it prints the reversed string. Finally, it prints the uppercase of the first letter of the reversed string using the toupper function from the ctype.h library.
Solution 2
Sure, here is a simple C program that accomplishes this:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void reverse_print(char *str) {
int length = strlen(str);
for(int i = length - 1; i >= 0; i--) {
printf("%c", str[i]);
}
printf("\n%c", toupper(str[length - 1]));
}
int main() {
char word[] = "word";
reverse_print(word);
return 0;
}
This program works as follows:
-
It includes the necessary header files.
stdio.hfor input/output functions,string.hfor string manipulation functions, andctype.hfor character manipulation functions. -
It defines a function
reverse_printthat takes a string as input. This function first calculates the length of the string usingstrlen. Then it uses a for loop to print the characters of the string in reverse order. After printing the reversed string, it prints the uppercase version of the first character of the reversed string usingtoupper. -
In the
mainfunction, it defines a stringwordand callsreverse_printwith this string. Themainfunction then returns 0 to indicate successful execution of the program.
Similar Questions
Write a program that takes a string of lowercase characters as input and prints it in uppercase, reversed.
Write a C program to find reverse of a string using pointers
Write a C function, which reverses a given string and returns a pointer to reversed string.char *reverse(char *str);
Write c function to reverse words in sentence (without using external arrays)
python program to reverse a string
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.