C

Reverse a String In C Using For Loop

In this tutorial, we are going to see how to reverse a string in C using for loop. For example, if a user enters the string “StackHowTo”, it will be “oTwoHkcatS” when reversed. A string that stays the same when reversed is a string named Palindrome.

There are four ways to reverse a string in C, by using for loop, pointers, recursion, or by strrev function.
100-multiple-choice-questions-in-c-programming100 Multiple Choice Questions In C Programming – Part 1This collection of 100 Multiple Choice Questions and Answers (MCQs) In C Programming : Quizzes & Practice Tests with Answer focuses on “C Programming”.  …Read More

Reverse a String In C Using For Loop
#include <stdio.h>
 
int main()
{
    char str[100], rev[100];
    int t, i, j;
 
    printf(" Enter a string :  ");
    gets(str);
    
    j = 0;
    t = strlen(str);
 
    //the last character must always be equal to '\0'.
    rev[t] = '\0'; 
    for (i = t - 1; i >= 0; i--)
    {
      rev[j++] = str[i];
    }
    rev[i] = '\0';
 
    printf(" String after reversing it: %s", rev);
    
    return 0;
}

Output:

 Enter a string :  StackHowTo
 String after reversing it: oTwoHkcatS

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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