C++ prob w/ functions other than Main...

My friend told there's a way to write string manipulation functions without using strings or arrays, then toyed with me by sending this:

/*
+-----------------------------------------------------------------------------+
|Name: |
| main |
|Parameters: |
| int argc: Count of the command line arguments passed by |
| the operating system. |
| char **argv: Array of strings. argv[0] is the program name, |
| argv[1] is the first command line argument. |
|Return: |
| int s: Integer indicating exit status. 0 = normal exit.|
|Description: |
| The "main" function is the entry point for C++ programs. |
| Every C++ program must have a "main" function, and this is |
| where execution always starts. |
+-----------------------------------------------------------------------------+
*/
int main(int argc, char **argv)
{
char userHoldCharacter; /* Input character from user */
char userOption; /* User option */
int userShift; /* Csesar Shift Amount */

do
{
cout << "\nEnter one of the following:\n\
\tQ - Quit the program\n\
\tC - Caesar Shift A Line\n\
\tF - Flip Case Of A Line\n\
\tR - Reverse Characters In A Line\n\
\n\tEnter Option: ";
userOption = firstCharUpperCase();
switch(userOption)
{
case 'Q':
break;
case 'C':
cout << "Enter shift amount: ";
cin >> userShift;
cout << "Shift is " << userShift
<< ". Enter a line of characters: ";
cin.ignore(8192,'\n');
while((userHoldCharacter = cin.get()) != '\n')
{
cout.put(caesarShift(userShift,userHoldCharacter));
}
cout.put('\n');
break;
case 'F':
cout << "Enter a line of characters: ";
while((userHoldCharacter = cin.get()) != '\n')
{
cout.put(flipCase(userHoldCharacter));
}
cout.put('\n');
break;
case 'R':
cout << "Enter a line of characters: ";
cout.put(reverseLine(getc(stdin)));
break;
default:
cout << "?Unrecognized option: \""
<< userOption << "\" " << endl << endl;
break;
}
} while (userOption != 'Q');

cout << "[normal exit]\n";
return 0;
}

...and told me "figure it out". After nights of scratching my head and tiresome research, I couldn't find a way to store and manipulate a string w/o strings or arrays. I was wondering if there even is a way, and if so could you show me how?
He's cheating. He's also just playing with semantics. Rather than getting an entire string at a time, he's just getting one character at a time.

Whether or not the characters are stored in an array doesn't mean he's not doing string operations.

For example, for the Caesar cipher, he:
1. gets a character from the user
2. if it is a newline, exit the loop
3. caesar-shift the character and print it out
4. loop (goto 1)

That is exactly the same process used on an array of characters (a "string"). The only difference is where the characters come from and go to. It is still a string operation.

Here is the caesarShift() function:
1
2
3
4
5
6
7
8
9
10
11
char caesarShift( signed char shift, char c )
  {
  if (isalpha( c ))  // only shift alphabetic characters
    {
    c += shift;
    if (!isalpha( c ))  // The result must be a letter, so wrap around if needed
      c -= 26;          // (for example 'Z'+3 --> 'C'; 'Y'+3 --> 'B'; etc)
    }
  else  // everything else remains unchanged
    return c;
  }


You can do the same sort of thing for flipCase(). Go ahead and write your own version of the function.

Now for the tricky one: reverseLine(). He has used a little misdirection on you by giving it an argument and a result. Both are strictly not necessary, but you can do it that way anyway.

The function should be recursive. Think about it a bit and try to write your own. You might find it helpful to first write a reverse function with the following prototype:
void reverseLine();

and in the main() function, change the line
cout.put(reverseLine(getc(stdin)));

to
reverseLine();

Once you have that working, kick it up a notch and rewrite it using the following prototype:

char reverseLine( char c );

Hint: the function is recursive, and it should return its argument unchanged.


I don't think it will cause you a problem, but if it does you'll want to change that line I indicated in main() to read:
cout.put(reverseLine(cin.get()));

Have fun!
I was thinking 4 the reverseLine there was a way to include a tag at the the end of whatever they put in, like EOF for files and then recursively read characters until I reach that tag and somehow display the reversed result...It's that "somehow" part that's getting to me (it's always easier said then done).
I got a task I need help ... :o(

Input Rectangular matrix A of mxn integers.

Rotate the elements of the matrix to the right by k elements by rows.

Input matrix 3x6

1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18

Matrix 3x6 after rotation:

18 1 2 3 4 5
6 7 8 9 10 11
12 13 14 15 16 17

Rotation is executed by p tprocesses. Each process executes rotation of (mxn)/p elements and it can use just one local auxiliary integer variable for rotation.

Output: Input and output matrix.


anyone can help !!
Last edited on
@steinzfan
The 'tag' is the same as for all the other functions: '\n'

@Bahakim
Please don't hijack someone else's thread. If you need help, start a new thread.
[notices bahakim going off topic]

Ok I too am doing a problem virtually exactly to this, and I am having trouble with it as well. In short I am stuck, and I am running out of time. the reverseline part isn't finished and I don't know how to finish it either. I need HELP I am stuck!!


#include <iostream>
#include <cstring>
using namespace std;
//this is the Caesarshift problem
char caesarShift( signed char shift, char c )
{
if (isalpha( c )) // only shift alphabetic characters
{
c += shift;
if (!isalpha( c )) // The result must be a letter, so wrap around if needed
c -= 26; // (for example 'Z'+3 --> 'C'; 'Y'+3 --> 'B'; etc)
}
else // everything else remains unchanged
return c;
}

//This is te flipcase problem
char flipCase(char letter) {
if(letter < 90)
letter += 32;
else
letter -= 32;

return(letter);
}

//This is the reverseline problem
char reverseline(char back)
{
char store;
if (back != '\n');
store = back;
cout.put(reverseLine(getc(stdin)));
return store;

}
int main(int argc, char **argv)
{
char userHoldCharacter; /* Input character from user */
char userOption; /* User option */
int userShift; /* Caesar Shift Amount */

do
{
cout << "\nEnter one of the following:\n\
\tQ - Quit the program\n\
\tC - Caesar Shift A Line\n\
\tF - Flip Case Of A Line\n\
\tR - Reverse Characters In A Line\n\
\n\tEnter Option: ";
userOption = firstCharUpperCase();
switch(userOption)
{
case 'Q':
break;
case 'C':
cout << "Enter shift amount: ";
cin >> userShift;
cout << "Shift is " << userShift
<< ". Enter a line of characters: ";
cin.ignore(8192,'\n');
while((userHoldCharacter = cin.get()) != '\n')
{
cout.put(caesarShift(userShift,userHoldCharacter));
}
cout.put('\n');
break;
case 'F':
cout << "Enter a line of characters: ";
while((userHoldCharacter = cin.get()) != '\n')
{
cout.put(flipCase(userHoldCharacter));
}
cout.put('\n');
break;
case 'R':
cout << "Enter a line of characters: ";
cout.put(reverseLine(getc(stdin)));
break;
default:
cout << "?Unrecognized option: \""
<< userOption << "\" " << endl << endl;
break;
}
} while (userOption != 'Q');

cout << "[normal exit]\n";
return 0;
}
Last edited on
You want to stop recursing if back is '\n', but you recurse indefinitely no matter what.
Topic archived. No new replies allowed.