Good day! I would like to ask on how to reverse arrays and putting it on another array of char?

   string str;
   int k=0, count = 0,namelength, acc=0;
   char name[80];
   char extract[80];
   char reversing[80];
   for(int j=0; j<strlen(extract);j++){
    reversing[strlen(extract) -j]= extract[j];
            }
        for(int w; w<=strlen(extract);w++){
    cout << reversing[w];
    }

Is it correct?

Easiest way is to use two pointers. The pointer to the source string startgs at the beginning of the string. The pointer to the destination string starts at the beginning of the destination character array plus the length of the source string + 1. You need to the +1 in order to add the NULL string terminating character.

After setting that up, just create a while loop that loops until the source pointer reaches '\0'. On each loop iteration you increment the source string pointer but decrement the destination string pointer.

You can accomplish the same thing by using two integers instead of two pointers, such as i and j. Initialize i with 0, and j with strlen(source string) + 1. DO NOT calculate strlen() on each loop iteration because that takes too much computer time. Just calculate once before the loop is started, from then on increment i and decrement j.

I already tried this.

string str;
   int k=0, count = 0,namelength, acc=0;
   char name[80];
   char extract[80];
   char reversing[80];
   for(int j=0; j<strlen(extract);j++){
    reversing[k]= extract[j];
    k++
            }
        for(int w; w<=strlen(extract);w++){
    cout << reversing[w];
    }

But the problem is when I tried to run it the output is just the first char.

First of all, extract has to contain a valid string. What you have is a chracter array that contains random junk. Initialize it something like this:

char extract[80] = "Hello World";

Read the instructions I posted carefully, the last paragraph is what you want. You did not initialize the two integers correctly. And don't put strlen() inside the loop!

for(int j = 0; extract[j] != '\0'; j++)

I was doing a palindrome. But this time its a sentence.

string str;
   int k=0, count = 0,namelength, acc=0;
   char name[80];
   char extract[80];
   char reversing[80];
   size=strlen(name);
   for(int j=size; j<=0;j--){
    cout << reversing[j];
    q++;
    }

How about now?

The string is still uninitialized garbage. How do you plan to test your program to find out if it works or not?

How about now?

Don't ask me -- test it yourself on your computer and see if it does the job correctly or not. That's called "unit testing" and "debugging". It's your job to do those things.

In case you do not desire to do mathetics with your reversed array better and less prone to mistakes will be using string class member functions which already provide a proper mechanism for reversing characters.
The member function I am talking about is rbegin(). The code is pretty simple have a look at it.

#include <iostream>
#include <string>
using namespace std;


int main ()
{

    string str,str1;

    cout << "Input string of your choice :";
    getline(cin,str);

    for (string::reverse_iterator rit=str.rbegin();rit!=str.rend(); ++rit)
    {
            str1+=*rit;
    }

    cout << str1;
    return 0;
}
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.