Reverse Of String and Digits
Welcome to this tutorial where you will learn how to reverse strings and digits using the C programming language. Reversing a string or a set of digits is a common task in programming, and it is essential to have this skill as it is required in many programming tasks.
In this tutorial, we will provide you with a comprehensive guide to reversing strings and digits using the C programming language. We will cover different methods to perform this operation, including using loops, arrays, and functions.
You will learn how to write a C program to reverse strings and digits and we will provide you with easy-to-understand examples and clear explanations of the process.
For example, we will show you how to take a string like "codingfizz" and reverse it to "zzifgnidoc", and how to reverse a set of digits like "23456" to "65432".
By the end of this tutorial, you will have a strong understanding of how to reverse strings and digits using C programming language, and you will be able to implement this skill in your future programming tasks.
Reverse a String in C language:
There are many ways to print a reverse string using C language.
- Reverse a string using strrev() function.
#include <stdio.h>
#include <string.h>
int main(){
char str[50];
printf("Enter a String: ");
scanf("%s",&str);
printf("Reversed of given String: %s",strrev(str));
return 0;
}
Output:
Enter a String: codingfizz
Reversed of given String: zzifgnidoc
You can also reverse digits with the help of the above code.
#include <stdio.h>
#include <string.h>
int main(){
char str[50];
printf("Enter a String: ");
scanf("%s",&str);
printf("Reversed of given String: %s",strrev(str));
return 0;
}
Output:Output:
Enter Digits: 23425
Reversed of given Digits: 52432
- Reverse a string using loops.
#include <stdio.h>
#include <string.h>
void main()
{
char str[50], temp;
int i, l, r, len;
printf (" \n Enter a string: ");
scanf( "%s", &str);
len = strlen(str);
l = 0;
r = len - 1;
for (i = l; i <r; i++)
{
temp = str[i];
str[i] = str[r];
str[r] = temp;
r=r-1;
}
printf ("Reversed of given String: %s ", str);
}
Output:
Enter a String: codingfizz
Reversed of given String: zzifgnidoc
You can also reverse digits with the help of the above code.
Output:
Enter Digits: 23425
Reversed of given Digits: 52432
Reverse a Digits in C language:
#include <stdio.h>
#include <string.h>
void main()
{
int num, digit, reverse=0;
printf("Enter Digits: ");
scanf("%d",&num);
while(num>0){
digit=num%10;
reverse=(reverse*10)+digit;
num=num/10;
}
printf ("Reversed of given Digits: %d ", reverse);
}
0 Comments