Write C++ program to reverse a string enter by user


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. #define MAX_SIZE 100 //Maximum size of the string
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int i, j, lenght;
  10. char string[MAX_SIZE];
  11. char reverse[MAX_SIZE];
  12.  
  13. cout<<"Enter a string: ";
  14. cin>>string;
  15.  
  16. lenght = strlen(string);
  17. j = 0;
  18.  
  19. for(i=lenght-1; i>=0; i--)
  20. {
  21. reverse[j] = string[i];
  22. j++;
  23. }
  24. reverse[j] = '\0';
  25.  
  26. cout<<reverse;
  27.  
  28. return 0;
  29.  
  30. }
  31.  

Result

C++ language techstudy.org

     





Related Topics




Popular Posts