C Examples

WAP in C to copy a string into another in reverse order inputted by user

The Program in C to copy a string into another in reverse order inputted by user is given below:

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

int main() {
    char str1[100], str2[100];
    int i, len;

    printf("Enter the string: ");
    scanf("%s", str1);

    len = strlen(str1); //getting the length of the input string

    for (i = 0; i < len; i++) {
        str2[i] = str1[len-i-1]; //copying the characters in reverse order
    }
    str2[i] = '\0'; //adding null character at the end

    printf("The reversed string is: %s", str2);

    return 0;
}

Output:

Enter the string: interesting
The reversed string is: gnitseretni

Pro-Tips💡

This program first declares two character arrays (strings) of size 100 for storing the input string and the reversed string respectively.

Then it uses the scanf() function to prompt the user to enter the string, which is stored in the str1 array. The strlen() function is used to get the length of the input string.

Then, the program uses a for loop to copy the characters of the input string into the second string, but in reverse order.

The str2[i] = str1[len-i-1]; copies the characters in reverse order, and str2[i] = ‘\0’; adds a null character at the end of str2.

Finally, the program uses the printf() function to print out the reversed string stored in str2.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X