Strings are one of the most common data types in programming. They are sequences of characters that can represent words, sentences, names, passwords, and more. In this program, we will show you how to write a C program that reverses a string using a loop. We will also explain the logic behind the code and give some examples of the output.
C Program
Here is a possible C program to print the following characters in reverse.
#include <stdio.h>
#include <string.h>
int main()
{
char s[100]; // a string to store the input
int i; // a loop variable
int len; // the length of the input
printf("Enter a string: ");
scanf("%s", s); // read the input
len = strlen(s); // get the length of the input
printf("The reversed string is: ");
for (i = len - 1; i >= 0; i--) // loop from the last character to the first
{
printf("%c", s[i]); // print the character at index i
}
printf("\n");
return 0;
Explanation
1. The first step is to include the necessary header files. We need stdio.h for input and output functions, and string.h for string manipulation functions.
#include <stdio.h>
#include <string.h>
2. Next, we declare a variable to store the input string. We use char data type and an array of size 100 to allow up to 99 characters. We also declare a loop variable and a variable to store the length of the input string.
char s[100]; // a string to store the input
int i; // a loop variable
int len; // the length of the input
3. Then, we prompt the user to enter a string and read it using scanf function. We use %s format specifier to read a string without spaces.
printf("Enter a string: ");
scanf("%s", s); // read the input
4. Next, we use strlen function to get the length of the input string. This function returns the number of characters in the string before the null terminator.
len = strlen(s); // get the length of the input
5. Then, we print a message to indicate the reversed string and start a loop from the last character of the input string to the first character. We use len - 1 as the initial value of i, because arrays start from index 0 in C. We use i >= 0 as the condition to stop the loop, because we want to include the first character. We use i-- as the update statement to decrement i by 1 in each iteration.
printf("The reversed string is: ");
for (i = len - 1; i >= 0; i--) // loop from the last character to the first
6. Inside the loop, we print the character at index i using printf function and %c format specifier.
{
printf("%c", s[i]); // print the character at index i
}
7. After the loop, we print a new line character to end the output. Finally, we return 0 to indicate successful execution of the program.
return 0;
Output
Here is an example of how our program works. Suppose we enter hello as the input string. Then our program prints:
Enter a string: hello
The reversed string is: olleh
This means that our program has reversed the input string by printing its characters from right to left.
Here is another example. Suppose we enter 12345 as the input string. Then our program prints:
Enter a string: 12345
The reversed string is: 54321
This means that our program can also reverse numerical strings or any other strings that do not contain spaces.
Conclusion
In this program, we have learned how to write a C program that reverses a string using a loop. We have also learned how to use scanf, strlen and printf functions to handle strings in C. We hope you found this tutorial helpful and informative. If you have any questions or feedback, please leave a comment below. Thank you for reading!
We love your feedback and invite you to comment on our articles, exercises, examples, quizzes and others. Your feedback helps us make our content awesome and serve you better. Please leave a comment and tell us what you think. How did our content help you learn something new? Thank you for being a part of our community!