answersLogoWhite

0


Best Answer

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

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

#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;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program in c plus plus to reverse the sentence?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How you write a program in c plus plus to print plaindromic numbers from 1 to n?

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); }


A program c plus plus on automorphic numbers or not?

how to write a program that counts automorphic number from 1 to 999


How do you write a C plus plus program that will display the first 10 positive prime numbers?

By learning how to program on C+.


Write a program in c plus plus to implement macro processor?

Don't write, it is already written, google for 'cpp'.


How do you write program to convert meter to kilometer in c plus plus?

Divide it by 1000.


Do I need to write a program to find a substring in a given string in c plus plus?

No.


How do you write a multiplication sentence of 4 plus 4 plus 3?

Four plus four plus three?


How do you make sentence with right plus write?

I am still able to write with my right hand.


How do you write an Algorithm for a C plus plus Program?

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.


Write a program in c plus plus to compute first of non-terminal?

there is no solution of this problem...........that's it..........


How many classes can we write in a single c plus plus program?

Its limited only by available memory.


How do you write a C plus plus program that displays a pyramid of Xes on the screen using a for loop?

printf ("x")