The code below runs fine but I want some help how can I get output of my original input and reverse of that i.e. ABCDE Z. WXYZ ---> EDCBA .Z ZYXW The below is the code for this.

#include <iostream>
#include <cstring>

using namespace std;
void swap(char& a, char& b)
{
  char tmp = a;
  a = b;
  b = tmp;
}
int main()
{
  char name[19] = "ABCDE Z. WXYX";
  int a = -1, b = -1;
  int nameLength = strlen(name);
                 for (int i = 0; i <= nameLength; ++i)
                     if (i == nameLength || name[i] == ' ')
                     {
                     b = i;
                 for (int j = 0; j < (b - a) / 2; ++j)
                     swap(name[j + a], name[b - j - 1]);
                     a = b + 1;
                     }
                     else
                     if (a == -1)
                     a = i;
                     cout << name <<"\n";

   system("PAUSE");
   return EXIT_SUCCESS;
}
Nick Evan commented: http://www.daniweb.com/forums/misc-explaincode.html -2

Recommended Answers

All 3 Replies

If this is what you are looking for :

enter a word :   hello
hello backwords is :  olleh

then all you have to do is use a for loop, start from strlen(str)-1 , to >= 0 and print out at index i.

strlen and char-arrays in C++? :icon_eek:

Why not use the functions provided to us:

#include <iostream>
#include <string>
#include <algorithm>

int main()
{
    std::string str = "hello!";
    std::cout << str << '\n';
    std::reverse(str.begin(), str.end());
    std::cout << str << '\n';
}

Done :icon_smile:

>then all you have to do is use a for loop, start from
>strlen(str)-1 , to >= 0 and print out at index i.

It's not that simple. Read the question more carefully. What the OP wants is word reversal within a sentence, where a word is a sequence of non-whitespace characters, and a sentence is a sequence of zero or more words. You need to locate each word and reverse it individually, which is a smidge more complicated than just using a for loop (or calling std::reverse):

#include <algorithm>
#include <iostream>
#include <string>

std::string add_delimiters ( 
  const std::string& s, 
  std::string::size_type begin,
  std::string::size_type end, 
  char begin_delim_char,
  char end_delim_char )
{
  return s.substr ( 0, begin ) +
    begin_delim_char + s.substr ( begin, end - begin ) +
    end_delim_char + s.substr ( end );
}

int main()
{
  const std::string whitespace ( " \t\n" );

  std::string s ( "ABCDE Z. WXYX" );
  std::string::size_type begin = 0;

  std::cout<< s <<'\n';

  while ( begin != std::string::npos ) {
    std::string::size_type end = s.find_first_of ( whitespace, begin );

    if ( end == std::string::npos )
      end = s.size();

    std::reverse ( s.begin() + begin, s.begin() + end );
    std::cout<< add_delimiters ( s, begin, end, '>', '<' ) <<'\n';

    begin = s.find_first_not_of ( whitespace, end );
  }
}

I'm reasonably confident that the OP isn't allowed to use the more powerful parts of the standard library used in this example, but it's relatively simple to convert it to a more manual process.

Be a part of the DaniWeb community

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