Reverse a String Program

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to write a C program to reverse a given string. The program will take input from the user, reverse the string and output the result to the console. This concept can be used to check if a string is a palindrome, because a palindrome string will have the same value even after we reverse it.

Note: You need to create the file ~/project/main.c yourself to practice coding and learn how to compile and run it using gcc.

cd ~/project
## create main.c
touch main.c
## compile main.c
gcc main.c -o main
## run main
./main

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/while_loop("`While Loop`") c/ControlFlowGroup -.-> c/for_loop("`For Loop`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123323{{"`Reverse a String Program`"}} c/variables -.-> lab-123323{{"`Reverse a String Program`"}} c/data_types -.-> lab-123323{{"`Reverse a String Program`"}} c/operators -.-> lab-123323{{"`Reverse a String Program`"}} c/while_loop -.-> lab-123323{{"`Reverse a String Program`"}} c/for_loop -.-> lab-123323{{"`Reverse a String Program`"}} c/user_input -.-> lab-123323{{"`Reverse a String Program`"}} c/function_parameters -.-> lab-123323{{"`Reverse a String Program`"}} c/function_declaration -.-> lab-123323{{"`Reverse a String Program`"}} end

Declare variables and read input from the user

#include <stdio.h>
#include <string.h>

int main() {
   char str[1000], rev[1000];
   int i, j, count = 0;

   printf("Enter a string: ");
   scanf("%s", str);

In this step, we declare two char arrays str and rev, and three integer variables i, j and count. str is used to store the original string input by the user and rev is used to store the reversed string. count is used to keep track of the length of the string. We then prompt the user to input a string using printf and read it using scanf.

Calculate the length of the string

while (str[count] != '\0') {
   count++;
}
j = count - 1;

In this step, we use a while loop to traverse the original string until a null character \0 is reached. On each iteration, we increment the count variable to count the number of characters in the string. We then assign the value of count - 1 to j because arrays start at index 0 and we want j to be the index of the last character in the original string.

Reverse the string

for (i = 0; i < count; i++) {
   rev[i] = str[j];
   j--;
}
printf("Reversed string: %s\n", rev);

In this step, we use a for loop to iterate through the original string. On each iteration, we assign the character at index j of the original string to the corresponding index i in the reversed string. We then decrement j and repeat the process until we have reversed the entire string. Finally, we output the reversed string to the console using printf.

Full code

#include <stdio.h>
#include <string.h>

int main() {
   char str[1000], rev[1000];
   int i, j, count = 0;

   printf("Enter a string: ");
   scanf("%s", str);

   while (str[count] != '\0') {
      count++;
   }
   j = count - 1;

   for (i = 0; i < count; i++) {
      rev[i] = str[j];
      j--;
   }
   printf("Reversed string: %s\n", rev);

   return 0;
}

Summary

In this lab, you learned how to write a C program to reverse a given string. You learned how to calculate the length of a string using a while loop and how to reverse a string using a for loop and an index variable. Writing programs to manipulate strings is an important skill in C programming, and by mastering it, you can accomplish many useful tasks.

Other C Tutorials you may like