Hello, today I'm doing the exercise from the forum called "Strings are your friends until they betrayed you"
I have finished it and my code work nice,
But I want a professional or experienced programmer in here to check at my code, and probably give me some advice about my code, maybe there are more elegant or more simple code to solve this exercise, I will appreciate anything even if it's just a reply. thanks Here's my code :
// Exercise 9 - SAYFUTBY level 2 stars
// ** Modify the Program so that it reverses the users name
// i.e.
// John Smith -> htimS nhoJ
#include <iostream>
usingnamespace std;
// initializing function
void replacer(int iNameSize, string sName);
int main()
// initializing and declaring variable
{ string sFname, sLname, sFullName;
// Ask the user to input name
cout << "Enter your first name: "
<< endl;
cin >> sFname; cin.ignore();
cout << "Enter your last name: "
<< endl;
cin >> sLname; cin.ignore();
sFullName = sFname + " " + sLname;
int iNameLenght = sFullName.size();
// The replacement
cout << "Welcome";
replacer(iNameLenght, sFullName); // Call function
cout << ". What a weird name of you or.. Is your keyboard broken ?.";
return 0;
}
void replacer(int iNameSize, string sName)
{
for ( int j=iNameSize; j>=0; j--)
{ cout << sName[j]; } // reverse the user name by print looping it inverted
}
#include <iostream>
#include <string>
usingnamespace std;
#include <conio.h>
void clearkeybuff();
int main()
// initializing and declaring variable
{ string sFname, sLname, sFullName;
// Ask the user to input name
cout << "Enter your first name: "
<< endl;
cin >> sFname; clearkeybuff();
cout << "Enter your last name: "
<< endl;
cin >> sLname; clearkeybuff();
sFullName = sFname + " " + sLname;
// The replacement
cout << "Welcome ";
cout<<string(sFullName.rbegin(),sFullName.rend()); //the predefined way to reverse a string
cout << ". What a weird name of you or.. Is your keyboard broken ?.";
return 0;
}
void clearkeybuff() //clears all key events in the console input buffer except shift,ctrl,alt
{
while(_kbhit()) _getch();
}
Don't use <conio.h> its non standard.
Apart from that, include the <string> header so you can use other string-related functions which would help. One notable one which you could use is std::getline(), which allows you to retrieve a full name from the console, even if they use spaces within the name (prevent you from having to concatenate the names, and allows for middle names or the like).
As for your 'replacer' function, you don't need the parameter for the length of the string (you can get that directly from the string with size), and maybe it would be better to copy the string into another string and then return it (for a more generic use option). Alternately, you could use @SorinAlex's option of using the reverse begin / end of the string to construct another one, or you could use <algorithm>'s std::reverse function to directly reverse the string itself.
At any rate, your replacer function could cause a segmentation fault. Remember that arrays (and std::string can act like one) are in the range of 0 <= x < n? You probably wanted to start the loop from iNameSize-1.
a) #include string header. Always include headers for classes and functions you use even if everything appears to be working without them.
b) string have builtin method go get it size, you don't need to pass size as a parameter
c) You don't have to use return 0 in main. It is implied here
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
int main()
{
std::string temp;
std:: cout << "Enter your first name: ";
std::getline(std::cin, temp);
//↑allow my friend with first name Rosa Maria to use program
std::string fullName = temp + " ";
std::cout << "Enter your last name: ";
std::getline(std::cin, temp);
fullName += temp;
std::cout << "Welcome, ";
std::copy(fullName.crbegin(), fullName.crend(),
std::ostream_iterator<char>(std::cout));
std::cout << ". What a weird name of you or.. Is your keyboard broken ?";
}
Enter your first name: Wernher
Enter your last name: von Braun
Welcome, nuarB nov rehnreW. What a weird name of you or.. Is your keyboard broken ?