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.
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.
Solution
Here's a C program that accomplishes the task of printing a string in reverse using a character pointer and also prints the uppercase of the first letter of the reversed string.
#include <stdio.h>
#include <string.h>
void printReverseAndUppercaseFirstLetter(char *str) {
int length = strlen(str);
char *ptr = str + length - 1; // Point to the last character
// Print the string in reverse
printf("Reversed String: ");
for (int i = length - 1; i >= 0; i--) {
putchar(*(ptr--));
}
putchar('\n');
// Print the uppercase of the first letter of the reversed string
if (length > 0) {
char firstLetter = *(str + length - 1); // First letter of reversed string
printf("Uppercase of first letter: %c\n", firstLetter >= 'a' && firstLetter <= 'z' ? firstLetter - 32 : firstLetter);
}
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Remove newline character if it's read by fgets
str[strcspn(str, "\n")] = '\0';
printReverseAndUppercaseFirstLetter(str);
return 0;
}
Explanation of the Program:
- Input String: The program takes an input string from the user using
fgets
. - Reverse Printing: It uses a character pointer that points to the end of the string and prints the characters in reverse order.
- Uppercase Conversion: The program checks if the first letter of the reversed string (which is the last character of the original string) is lowercase. If it is, it converts it to uppercase by subtracting 32 (the ASCII difference between lowercase and uppercase letters).
- Output: It prints the reversed string and the uppercase of the first letter.
How to Compile and Run:
- Save the code in a file named
reverse_string.c
. - Open a terminal and navigate to the location of the file.
- Compile the program using:
gcc -o reverse_string reverse_string.c
- Run the program using:
./reverse_string
This program works for any string input and correctly handles the reverse and case conversion tasks as specified.
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 read an English Alphabet through keyboard and display whetherthe given alphabet is in upper case or lower case.
Write a C program to read a sentence and replace lowercase characters with uppercase and vice versa.
Find output of the below program:int main(){ char str[]={'e','a','r','t','h'}; printf("%s", str); return 0;}
Which among the following methods is used to reverse the sequence of characters in a StringBuilder object?a)backwards()b)invert()c)reverse()d)flip()
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.