The simplest solution is to use a template function that will reverse an array of any type. This is achieved by iteratively working from both ends of the array, swapping characters while the left index is less than the right. The following example demonstrates the function using both a character array and a std::string, but the function will also work with an array of any type.
#include<iostream>
#include<string>
template<class T>
void rev_array(T A[], size_t size )
{
size_t left=0, right=size-1;
while( left<right )
{
T tmp=A[left];
A[left]=A[right];
A[right]=tmp;
++left; --right;
}
}
int main()
{
char arr[] = "Hello world!";
std::cout<<arr<<std::endl;
rev_array(arr, strlen(arr));
std::cout<<arr<<std::endl;
std::cout<<std::endl;
std::string str = "The cat sat on the mat.";
std::cout<<str.c_str()<<std::endl;
rev_array(&str[0], str.size() );
std::cout<<str.c_str()<<std::endl;
std::cout<<std::endl;
return(0);
}
Example output:
Hello world!
!dlrow olleH
The cat sat on the mat.
.tam eht no tas tac ehT
#include<stdio.h>
#include<string.h>
int main()
{
char word[100],rev_word[100];
int l,i, j=0, v=0;
printf("Please enter a word: ");
gets(word);
l = strlen(word);
for(i=l-1; i>=0; i--){
rev_word[j]=word[i];
j++;
}
rev_word[j] = '\0';
printf("word = %s\n", word);
printf("reverse word = %s\n", rev_word);
system("pause");
return 0;
}
how to write a program that counts automorphic number from 1 to 999
Don't write, it is already written, google for 'cpp'.
Divide it by 1000.
there is no solution of this problem...........that's it..........
Its limited only by available memory.
To check if a number is a palindrome, reverse the number and see if it is equal to the original number. If so, the number is a palindrome, otherwise it is not. To reverse a number, use the following function: int reverse(int num, int base=10) { int reverse=0; while( num ) { reverse*=base; reverse+=num%base; num/=base; } return(reverse); }
how to write a program that counts automorphic number from 1 to 999
By learning how to program on C+.
Don't write, it is already written, google for 'cpp'.
Divide it by 1000.
No.
Four plus four plus three?
I am still able to write with my right hand.
You don't write an algorithm for a C++ program, unless you are documenting the C++ program after-the-fact. The normal procedure is to write the algorithm first, in a language independent fashion, and then translate that stated algorithm into C++ code, or into whatever language you wish.
there is no solution of this problem...........that's it..........
Its limited only by available memory.
printf ("x")