Question:
Pointers in C++, using c-string! I wasn't sleeping in class!?
Randy Fitzgerald
2011-04-21 13:09:19 UTC
Write a function that takes a C string as an input parameter and reverses the string. The function
should use two pointers, 'front' and 'rear'. The 'front' pointer should initially reference the first
character in the string, and the 'rear' pointer should initially reference the last character in the
string. Reverse the string by swapping the characters referenced by 'front' and 'rear', then increment
front to point to the preceding character, and so on, until the entire string is reversed. Write a
main program to test your function on various strings of both even and odd length......

SOURCE CODE:

#include
#include

using namespace std;

const int MAXIMUM_LENGTH = 100;

void swap(char a[], int size);

int main()
{
char userEntry[MAXIMUM_LENGTH];

cout << "Enter a string of characters and I will reverse them: \n" < cin.getline(userEntry, MAXIMUM_LENGTH);

char *words;

words = new char[MAXIMUM_LENGTH];
int length = strlen(userEntry);
cout << "\nThe length of the string is " << length << " characters.\n";

swap(userEntry, length);

return 0;
}

void swap(char a[], int length)
{
int i = 0;
char *rear;
char *front;
rear = &a[i];
*rear = a[i];
front = &a[i];
*front = a[i];
for(i = 0; i < length; i++)
{
*rear = a[length-1];
}
cout << *rear; //JUST A DRIVER


}

Any hints/suggestions!? I think it's just the fact of a poor algorithm.
Four answers:
Cubbi
2011-04-21 13:38:38 UTC
Since this is C++, despite the use of character arrays as if they were strings, you can swap the pointed-to objects using iter_swap()



#include

#include

#include



const int MAXIMUM_LENGTH = 100;



void swap(char a[], int size);



int main()

{

     std::cout << "Enter a string of characters and I will reverse them: \n";

     char userEntry[MAXIMUM_LENGTH];

     std::cin.getline(userEntry, MAXIMUM_LENGTH);

     int length = strlen(userEntry);

     std::cout << "\nThe length of the string is " << length << " characters.\n";

     swap(userEntry, length);

     std::cout << "Swapped string: " << userEntry << '\n';

}



void swap(char a[], int length)

{

     char *front = a;

     char *rear = a + length - 1;

     for( ; front<=rear; ++front, --rear)

         std::iter_swap(front, rear);

}



test: https://ideone.com/iZmVA
DelMan
2011-04-21 13:59:02 UTC
I edited your code alittle ... here's it





#include

#include





const int MAXIMUM_LENGTH = 100;



void swap(char a[], int size);



void main()

{

char userEntry[MAXIMUM_LENGTH];



cout << "Enter a string of characters and I will reverse them: \n" <
cin.getline(userEntry, MAXIMUM_LENGTH);



char *words;



words = new char[MAXIMUM_LENGTH];

int length = strlen(userEntry);

cout << "\nThe length of the string is " << length << " characters.\n";



swap(userEntry, length);





}



void swap(char a[], int length)

{

int i = 0;

char *rear;

char *front;

rear = &a[i];

*rear = a[i];

front = &a[i];

*front = a[i];

int counter=length; // to count by



for(i=0; i


{

*rear=a[--counter];

cout<<*rear; /* here it displays the word reversed except for last character and this is driving me crazy really :@ .... your code for this loop would display only last character times length*/

}



}





BUT after all .... why dont you think about using stack? just like this





#include

#define maxstacksize 100

int top=-1;

char stack[maxstacksize];



void push(char letter)

{

if (top==maxstacksize-1)

{

cout<<"stack full...";

}

stack[++top]=letter;

}



char pop()

{

if (top==-1)

{

cout<<"stack empty...";

return 0;

}

return stack[top--];

}

void main()

{

char symbol;

cout << "Enter a line of characters and press enter..."<
cin.get(symbol);

while (symbol != '\n')

{

push(symbol);

cin.get(symbol);

}

while (top!=-1)

{

cout<
}



}





hope this helped :)
?
2011-04-21 13:44:59 UTC
How about this swapping algorithm?





void swap(char a[], int length)

{

char* pRear =&a[length-1];

char* pFront = a;



for(int i = 0; i < length/2; ++i)

{

char tmp = *pRear;

*pRear++ = *pFront;

*pFront++ = *pRear;

}



cout << *pRear;
?
2016-10-27 13:21:53 UTC
guidelines are undemanding, truly. you comprehend, a "hi" string is saved as "H","e","l","l","o","0" characters interior the memory and the pointer is the tackle of the "H" personality, that's the starting up element of the string. What you want to do is to replica the records starting up on "source" into the position "buffer_start_address" factors. buffer_start_address[0] is our first personality int i; for(i=0;i


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...