Reverse a String Using Pointer

CCBeginner
Practice Now

Introduction

Pointers provide direct access to memory, and by using pointers, we can access and manipulate the values and addresses of variables and arrays in memory. The program we will build in this lab will use pointer variables to reverse a given string.

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/CompoundTypesGroup(["`Compound Types`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) 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/CompoundTypesGroup -.-> c/strings("`Strings`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123325{{"`Reverse a String Using Pointer`"}} c/variables -.-> lab-123325{{"`Reverse a String Using Pointer`"}} c/data_types -.-> lab-123325{{"`Reverse a String Using Pointer`"}} c/operators -.-> lab-123325{{"`Reverse a String Using Pointer`"}} c/while_loop -.-> lab-123325{{"`Reverse a String Using Pointer`"}} c/strings -.-> lab-123325{{"`Reverse a String Using Pointer`"}} c/user_input -.-> lab-123325{{"`Reverse a String Using Pointer`"}} c/pointers -.-> lab-123325{{"`Reverse a String Using Pointer`"}} c/function_parameters -.-> lab-123325{{"`Reverse a String Using Pointer`"}} c/function_declaration -.-> lab-123325{{"`Reverse a String Using Pointer`"}} end

Declare string variables

We will start by declaring two char arrays: one for the original string and one for the reversed string.

char str[100];
char rev[100];

Declare pointer variables

Now, we will declare two pointer variables: one to point to the first element of the string 'str', and the other to point to the first element of the reversed string 'rev'.

char *sptr = str;
char *rptr = rev;

Get input from the user

We will ask the user to input a string that they want to reverse using the scanf function:

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

Find the end of the string

We need to find the end of the string so that we can start traversing it in reverse order. We will use a while loop to increment the sptr until it points to the null character at the end of the string.

while(*sptr)
{
   sptr++;
}

Reverse the string

Now, we will use a while loop to traverse the string str in reverse order. We will decrement sptr to move from the null character to the last character of the string, and for each element in the string, we will store the value in the reversed string rev by using the * operator.

while(sptr >= str)
{
   *rptr = *sptr;
   rptr++;
   sptr--;
}

Add null character to end of reversed string

The end of the reversed string was reached. We must add the null character to the end of the reversed string:

*rptr = '\0';

Copy reversed string to original string

Now we will copy the reversed string to the original string.

strcpy(str, rev);

Print the reversed string

Finally, we will print out the reversed string.

printf("Reversed string: %s\n", str);

Summary

In this lab, we learned how to use pointer variables to reverse a given string. Pointers provide direct access to memory, and by using them, we can access and manipulate the values and addresses of variables and arrays in memory. We used a while loop to traverse the string in reverse order, stored the reversed string in a new character array, and added a null character to the end of the reversed string. Finally, we copied the reversed string to the original string and printed it out.

Other C Tutorials you may like