This is not giving right output :(((

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()  
{  
      
    char c[80],d[80];  
    cout<<"Enter a string = ";  
    cin.get(c,80);  
    strcpy(c,d);  
    strrev(d);  
         cout<<"String = "<<c;  
  
         cout<<"Reverse word "<<d;  
    getch();  
    return 0;
}

Recommended Answers

Its because strcpy works like this strcpy(<destination>,<source>) you reversed the destination with source that's why it didn't work.

Jump to Post

All 2 Replies

Its because strcpy works like this strcpy(<destination>,<source>) you reversed the destination with source that's why it didn't work.

Without using arrays and without using strings? That makes it interesting.
Something like a linked list would obviously work, but perhaps storing each character in the local frame of a (recursive) function would be the simplest.

#include <iostream>

void read_and_print_reverse()
{
    char c = std::cin.get() ;
    if( c != '\n' )
    {
        read_and_print_reverse() ;
        std::cout << c ;
    }
}

int main()
{
    std::cout << "enter a sentence (terminate it with a new-line): " ;
    read_and_print_reverse() ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.