Strings Are Your Friends Until They Betrayed You

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 :

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
// Exercise 9 - SAYFUTBY level 2 stars
// ** Modify the Program so that it reverses the users name
// i.e.
// John Smith -> htimS nhoJ
#include <iostream>

using namespace 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
    }



But I want a professional or experienced programmer in here to check at my code


No point in me looking at it then :)

All i'd say (at a quick glance) is don't use:

using namespace std;

http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
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
#include <iostream>
#include <string>
using namespace 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();
}

Both of you, thank you so much, I appreciate it.
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.
Last edited on
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

Slightly modified program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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 ?
Last edited on
Topic archived. No new replies allowed.