Hello everyone!
Well I, McClapYourHands, am utterly confused. I am working on a class project that is relating C-strings and pointers.
Part of the problem, is that the wording on this assignment confuses me to no end. Secondly, me and c-strings just don't get along and I am always mixing up what you can/cannot do.
The goal of this code is to do 3 things.
1) modify the given code to enact the next 2 goals.
2) Your program should ask the user for the size of the C-string to be entered, then use new to create the two pointers (C-strings).
3) write a function that reverses a string, to practice using pointers
Problems I'm running into/questions I cannot answer.
1. initially the STRINGSIZE was 'const char' type but then I couldn't use it in IntArray. but ofcourse, I'm running into a problem when using it to define the size of oldCString. ..... what am I not realizing??
2. I've gotten the reverse function to work... slightly. I get the words to be reversed but all sorts of weird symbols before and after it. I know i'm misunderstanding something basic.
My brain is not grasping these basic concepts, so I'm looking to anyone to help explain things differently. Thanks in advance!
code bellow
*****************************************************************************
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
typedef int* IntArrayPtr;
int Reverse(char * destination, const char * source, int num);
int main()
{
int STRINGSIZE;
char oldCString[];
char newCString[STRINGSIZE];
cout << "Enter the size of the C-string you are using:\n";
cin >> STRINGSIZE;
IntArrayPtr a;
a = new int[STRINGSIZE];
cout << "Enter the C-string you would like reversed. \n";
cin >> oldCString;
cout << endl << endl;
cout << "oldCString: " << oldCString << endl;
cout << "newCString before changing: " << newCString << endl;
Reverse(newCString, oldCString, STRINGSIZE); // testing your function...
cout << "newCString after Reverse: " << newCString << endl;
return 0;
}
// Reverses a C-string passed in (source), and places the reversed
// C-string into (destination).
// (num) should represent the maximum valid length of (destination).
// If no null-zero character is found in (source) within (num-1)
// characters,the function will only read up to (num-1) characters
// from (source), and then copy the reversed characters to
// (destination) and append a null-zero to the end of it.
// The function will return the number of characters placed into
// (destination), including the null-zero.
// The function MUST use pointer notation (not array notation) inside
// it. However, you might find it useful to use array notation
// temporarily while developing the function, and then replace
// with pointer notation before turning in the assignment.
int Reverse(char * destination, const char * source, int num)
{
for (int i = 0; i < num; i++)
{
destination[i] = source[i];
destination[i] = source[num - i - 1];
cout << destination[i];
}
return 0;
}
|