Object Oriented Programming - Old Questions
4. Write a member function called reverseit() that reverses a string ( an array of character). Use a for loop that swaps the first and last characters, then the second and next-to last characters and so on. The string should be passed to reverseit() as an argument.
5 marks
|
Asked in 2075
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void reverseit(char *p)
{
int j;
int len=strlen(p);
j=len-1;
for(int i=0;i<len/2;i++)
{
char a=p[i];
p[i]=p[j];
p[j]=a;
j--;
}
cout<<"\\n\\nReverse Of String :";
puts(p);
}
void main()
{
char str[50];
char ch;
cout<<"\\nEnter The String : ";
gets(str);
reverseit(str);
getch();
}