C++ printing a string in reverse - c++

This program works and does what is required. My question is in regards to the ReverseName function, and how it works exactly. I was able to reverse engineer it from some examples in my book, but I cant quite figure out how it works. I get Name is sent to it. i just dont understand how it is printed in reverse.
#include <iostream>
using namespace std;
void ReverseName(char *s );
int main(void){
char Name[] ="John Doe";
cout << "Name is: " << Name << "\n" << "Name Backwards is: " ;
ReverseName(Name);
cout << "\nName is: "<< Name << '\n';
return 0;
}
void ReverseName(char * s){
if(*s){
ReverseName(s+1);
cout << *s;
}
return;
}

void ReverseName(char * s){
if(*s){
ReverseName(s+1); // come into the end of the string
cout << *s;
}
return;
}
it just like this:
'J'
call --> 'o'
call ---> 'h'
call ---> ...
call ---> 'e' (Recursion to the end)
and when the stack come to the end of recursion, it will execute back in order. then it just like.
cout << 'e'
cout << ...
cout << 'h'
cout << 'o'
cout << 'J'
(come back from the call stack)

It's a recursive function.
It gets a pointer to a char array and calls itself until the the end of char array is reached, then it "unwinds" the recursion, printing all the chars from the back.

ReverseName() is a recursive function.
http://www.learncpp.com/cpp-tutorial/710-recursion/

Related

Pointer and char in struct

How to read the second letter in char with a pointer? I can read the whole message "carp" and first letter 'c' but I have no idea how to read second letter... Here is my example code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
struct list {
char name[20];
int length;
};
list first ={
"carp",
6,
};
list *p = &first;
cout << p->name << endl; // "carp"
cout << *p->name << endl; // "c"
p = p + 1;
cout << *p->name << endl; // Not working...How to read a?
return 0;
}
Incrementing p with p++ or p = p+1 moves you to the next instance of struct list, which is not what you want (and it's not even there).
Instead, you want to move to the second letter of name, which can be done in several ways:
Use index on the name - cout << p->name[1] << endl;
Make a pointer to p->name and increment it, i.e. char *np = p->name; np++; cout << *np
Use pointer arithmetic instead of indexing, i.e. cout << *(p->name+1) << endl;
You can use index on name to access any character:
p->name[1] // gives 'a'
p->name[2] // gives 'r'
Note that arrays begin with an index of 0. So p->name[0] would give 'c'.
p + 1 actually increments p, which is a pointer to list. This essentially moves to the next instance of list, which is not even initialized in your code.
Use the array subscript operator with an index of 1:
p->name[1];
If you want to output the second character using pointers without the subscript operator then you can just write
cout << *( p->name + 1 ) << endl;
It is the same as
cout << p->name[1] << endl;
Or you can introduce an intermediate pointer. For example
for ( const char *q = p->name; *q != '\0'; ++q )
{
cout << *q;
}
cout << endl;

Class object (char) changes after function

I'm pretty new to C++, but this has got me stumped. I'm working on the base code for an RPG, but this one character in the class has got me stumped. I've isolated the pieces at issue here (there's a good 1000 lines cut out), and the problem remains.
Here's the class and header for the program:
#include <iostream>
#include <cstdlib>
using namespace std;
unsigned long errorcount;
// I know this is bad coding, but it's not going to be in the end product...
class character {
public:
void setgender(char newgender);
char getgender() const;
private:
char gender;
};
void character::setgender(char newgender) {
switch (newgender) {
case 'M': gender = 'M'; break;
case 'F': gender = 'F'; break;
default: gender = '0'; errorcount++; break;
}
std::cout << "\nDuring setgender function: " << gender;
return;
}
char character::getgender() const {
std::cout << "\nDuring getgender function: " << gender;
return gender;
}
This next part that has me scratching my head. I started the following code:
void PlayerCharacterCreation(character Player) {
string newgender;
while(true) {
std::cout << "\nAre you male or female?" << "\n1. Male" << "\n2. Female" << "\n::";
std::cin >> newgender;
if (newgender == "1") { Player.setgender('M'); break; }
if (newgender == "2") { Player.setgender('F'); break; }
std::cout << "\nInvalid response. Try again.";
}
std::cout << "\nAfter setgender function: " << Player.getgender();
}
void PlayerCreationTest() {
character Test;
PlayerCharacterCreation(Test);
char playergender = Test.getgender();
if (playergender != 'M' && playergender != 'F') { errorcount++; }
std::cout << "\nAfter getgender function: " << playergender;
std::cout << "\n\nOUTPUT BEGINS NOW\nGender: " << playergender << "\n";
std::cout << "OUTPUT ENDS. Total Errors: " << errorcount << ".";
return;
}
int main() {
PlayerCreationTest();
return 0;
}
Now as far as I can tell, there's nothing wrong with any of this - the (GCC) compiler doesn't complain, and it works just fine up to a point. But if I run it, I get the following output:
Are you male or female?
1. Male
2. Female
1
During setgender function: M
During getgender function: M
After setgender function: M
During getgender function: #
After getgender function: #
OUTPUT BEGINS NOW
Gender: #
OUTPUT ENDS. Total Errors: 1.
Worse than that, if I choose option "2" the output is the same only when it makes no sense:
Are you male or female?
1. Male
2. Female
2
During setgender function: F
During getgender function: F
After setgender function: F
During getgender function: #
After getgender function: #
OUTPUT BEGINS NOW
Gender: #
OUTPUT ENDS. Total Errors: 1.
In other words, the expected output goes badly wrong somewhere between the last line of PlayerCharacterCreation(), and the very next line of the PlayerCreationTest().
As far as I can tell, though, the "character" class should stay the same between functions, not change all willy-nilly like this.
I hope that's enough for someone to figure out what I'm doing wrong, but I was toying with it a little and managed to change the output character even more.
By adding an "srand(0)" line at the beginning of the main function, I can change the '#' to a 'y' for both options 1 and 2.
By adding a "GenderTest()" line at the beginning of the main function, I can change the '#' to a 'F', for both options. If I add both lines, only the one immediately above the "PlayerCreationTest()" line seems to matter. Which is odd, because the full code always returns an 'l' (lowercase L) instead of '#', and the main function is exactly the same as written above.
As far as I can tell, though, the "character" class should stay the same between functions, not change all willy-nilly like this.
Well, you're wrong. They do stay the same, because they are seperate variables. PlayerCharacterCreation creates a local character (a copy of Test), and at the end of the function, the object is destroyed.
The original character that you passed to PlayerCharacterCreation never changed, and you get some weird output because the gender was never set for that character.
The Player in PlayerCharacterCreation is a totally new character, it is not Test :)
If you want to modify the character passed to PlayerCharacterCreation, you have to pass it by reference (there are some other ways too, like passing a pointer, returning Player, but that's the best one):
void PlayerCharacterCreation(character& Player);
^^^
reference
void PlayerCharacterCreation(character Player)
Inside this function, Player is a local instance of character into which the calling parameter is copied. Consider the following:
#include <iostream>
void f1(int x) {
x++;
}
void f2(int i) {
i++;
}
int main() {
int i = 0;
f(i);
std::cout << i << '\n';
}
We know that the output from this will be '0', because f1::x and f2::i are their own independent variables copied from our source parameter.
If you want to pass a specific instance of a variable rather than a copy of it, you need to provide a pointer or a reference.
void by_pointer(Character* player) {
if (player == nullptr) {
error_handling();
}
player->do_thing();
}
by_pointer(&player);
void by_reference(Character& player) {
player.do_thing();
}
by_reference(player);
Example:
#include <iostream>
int f1(int& param) {
param++;
}
int main() {
int i = 0;
f1(i);
std::cout << i << '\n'; // outputs 1
}

Sorting a sentence using arrays and strings

Sorry guys forewarning I suck at coding but have a big project and need help!
Input: A complete Sentence.
Output: The sorted order (ASCii Chart Order) of the sentence (ignore case.)
Output a histogram for the following categories:
1) Vowels
2) Consonants
3) Punctuation
4) Capital Letters
5) LowerCase Letters
I have no clue what to even do
Since you are vague in what your issue is, I recommend the following process:
Review Requirements
Always review the requirements (assignment). If there are items you don't understand or have the same understanding as your Customer (instructor), discuss them with your Customer.
Write a simple main program.
Write a simple main or "Hello World!" program to validate your IDE and other tools. Get it working before moving on. Keep it simple.
Here's an example:
#include <iostream>
#include <cstdlib> // Maybe necessary for EXIT_SUCCESS.
int main()
{
std::cout << "Hello World!\n";
return EXIT_SUCCESS;
}
Update program to input text & validate.
Add in code to perform input, validate the input and echo to the console.
#include <iostream>
#include <cstdlib> // Maybe necessary for EXIT_SUCCESS.
#include <string>
int main()
{
std::string sentence;
do
{
std::cout << "Enter a sentence: ";
std::getline(cin, sentence);
if (sentence.empty())
{
std::cout << "\nEmpty sentence, try again.\n\n"
}
} while (sentence.empty());
std::cout << "\nYou entered: " << sentence << "\n";
// Keep the console window open until Enter key is pressed.
std::cout << "\n\nPaused. Press Enter to finish.\n";
std::cin.ignore(100000, '\n');
return EXIT_SUCCESS;
}
Add functionality, one item at a time and test.
Add code for one simple requirement, compile and test.
After it works, make a backup.
Repeat until all requirements are implemented.
For ordering the string you can use standard c qsort function. For counting vowels, consonants, punctuation... you need a simple for loop.
Here is a working example:
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int cmp(const void* pc1, const void* pc2)
{
if(*(char*)pc1 < *(char*)pc2) return -1;
if(*(char*)pc1 > *(char*)pc2) return 1;
return 0;
}
void main(int argc, char* argv[])
{
char pczInput[2000] = "A complete sentence.";
cout << endl << "Input: '" << pczInput << "'";
qsort(pczInput, strlen(pczInput), sizeof(char), cmp);
cout << endl << "Result: '" << pczInput << "'";
int iCapital = 0;
int iLowerCase = 0;
int iPunctuation = 0;
int iVowels = 0;
int iConsonants = 0;
for(unsigned int ui = 0; ui < strlen(pczInput); ++ui)
{
if(isupper(pczInput[ui])) ++iCapital;
if(islower(pczInput[ui])) ++iLowerCase;
if(ispunct(pczInput[ui])) ++iPunctuation;
if(strchr("aeiouAEIOU", pczInput[ui]) != NULL) ++iVowels;
if(strchr("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ", pczInput[ui]) != NULL) ++iConsonants;
}
cout << endl << "Capital chars: " << iCapital;
cout << endl << "Lower case chars: " << iLowerCase;
cout << endl << "Punctuation chars: " << iPunctuation;
cout << endl << "Vowels chars: " << iVowels;
cout << endl << "Consonants chars: " << iConsonants;
cout << endl;
}
Note that I used C standard functions for counting capital, lower case and punctuation, and I had to use strchr function for counting vowels and consonants because such functions are missing in standard C library.
The output of the program is:
Input: 'A complete sentence.'
Result: ' .Acceeeeelmnnopstt'
Capital chars: 1
Lower case chars: 16
Punctuation chars: 1
Vowels chars: 7
Consonants chars: 10

Update: program shows adress of fstream instead of the text file

I am about to write a program which asks the user if they want to "search or convert" a file, if they choose convert, they need to provide the location of the file.
I do not know why the program shows the address of the file instead of opening it.
Here is my first approach:
#include <fstream>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
char dateiname[64], kommando[64];
ifstream iStream;
cout << "Choose an action: " << endl <<
" s - search " << endl <<
" c - convert" << endl <<
" * - end program" << endl;
cin.getline(kommando,64,'\n');
switch(kommando[0])
{
case 'c':
cout << "Enter a text file: " << endl;
cin.getline(dateiname,64,'\n');
iStream.open("C://users//silita//desktop//schwarz.txt");
case 's': break;
case '*': return 0;
default:
cout << "Invalid command: " << kommando << endl;
}
if (!iStream)
{
cout << "The file " << dateiname << " does not exist." << endl;
}
string s;
while (getline(iStream, s)) {
while(s.find("TIT", 0) < s.length())
s.replace(s.find("TIT", 0), s.length() - s.find("TIT", 3),"*245$a");
cout << iStream << endl;
}
iStream.close();
}
At first you can't compare c-strings using ==. You must use strcmp(const char*, const char*). More info about it you can find there: http://www.cplusplus.com/reference/cstring/strcmp/
For example: if (i == "Konvertieren") must become if(!strcmp(i,"Konvertieren"))
As mentioned in Lassie's answer, you can't compare strings in this way using c or c++; just to flesh it out, however, I'll explain why.
char MyCharArr[] = "My Character Array"
// MyCharArr is now a pointer to MyCharArr[0],
// meaning it's a memory address, which will vary per run
// but we'll assume to be 0x00325dafa
if( MyCharArr == "My Character Array" ) {
cout << "This will never be run" << endl;
}
Here the if compares a pointer (MyCharArr) which will be a memory address, ie an integer, to a character array literal. Obviously 0x00325dafa != "My Character Array".
Using cstrings (character arrays), you need to use the strcmp() function which you will find in the cstring library, which will give you a number telling you "how different" the strings are, essentially giving the difference a numerical value. In this instance we are only interested in no difference, which is 0, so what we need is this:
#include <cstring>
using namespace std;
char MyCharArr[] = "My Character Array"
if( strcmp(MyCharArr,"My Character Array")==0 ) {
// If there is 0 difference between the two strings...
cout << "This will now be run!" << endl;
}
While you are not doing so in your question, If we were using c++ strings rather than character arrays, we would use the compare() method to similar affect:
#include <string>
using namespace std;
string MyString = "My C++ String"
if( MyString.compare("My C++ String")==0 ) {
// If there is 0 difference between the two strings...
cout << "This will now be run!" << endl;
}

line.find won't compile, line is not declared

I am a very novice programmer, and I am trying to understand the find functions for strings. At uni we are told to use c-strings, which is why I think that it isn't working. The problem comes when I compile, there is a compile error that line was not declared. This is my code:
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
char test[256];
char ID[256];
cout << "\nenter ID: ";
cin.getline(ID, 256);
int index = line.find(ID);
cout << index << endl;
return 0;
}
Please help, it has become really frustrating as I need to understand this function to complete my assignment :/
You're trying to use C-style strings. But find is a member of the C++ string class. If you want to use C-style strings, use functions that operate on C style strings like strcmp, strchr, strstr, and so on.
Supposing you actually input some data into test also, then one way to do it would be:
char *found = strstr(test, ID);
if ( !found )
cout << "The ID was not found.\n";
else
cout << "The index was " << (found - test) << '\n';
Because find fuction a member function string class,You should declare a string class's object. I think you will do that like this:
string test = "This is test string";
string::size_type position;
position = test.find(ID);
if (position != test.npos){
cout << "Found: " << position << endl;
}
else{
cout << "not found ID << endl;
}