Code Review

Hows it going everyone!?

So I'm fairly new to C++ and loving it so far. I was wondering if there is a web site that people rate each others code and give each other advice?

I wrote these functions below. since I couldn't find any built in features to do the same things. For example. in python you have string.replace('what','with')

I want to know how I did and what I could do differently so I can improve my self.
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
//My make string to lower...
string makeLower(string theString){
    for (int i = 0; i != theString.length(); i++)
    {
        theString[i] = tolower(theString[i]);
    }
    return theString;
}


//Make String Reverse.
string make_reverse(string str) {
    stringstream newString;
    for (float i = str.length(); i != -1; i--){
        newString << str[i];
    }
    return newString.str();
}


//If String contains char.
bool strContains(string str, char let) {
    if (str.find(let) < str.length()){
        return true;
        
    }else {
    return false;
    }
}

//Remove String At Index
string removeAtIndex(string str, int index){
    stringstream newString;
    
    for (int i = 0; i != str.length(); i++){
        if (i != index){
            newString << str[i];
        }
    }
    return newString.str();
}
There are easier ways to accomplish those tasks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>

int main() {
	//make lower
	std::string a = "HELLO";
	std::transform(a.begin(), a.end(), a.begin(), ::tolower);
	std::cout << a << std::endl;
	
	//reverse string
	std::string b{a.rbegin(), a.rend()};
	std::cout << b << std::endl;
	
	//contains
	bool c = (b.find('e') != std::string::npos);
	std::cout << std::boolalpha << c << std::endl;
	
	//remove at index
	std::string s = "hello";
	s.erase(s.begin() +4); // or s.erase(4,1);
	std::cout<< s << std::endl;
}

http://ideone.com/gWjX4o
Last edited on
Hi,

First up, the for loop looks like this:

for (int i = 0; i < theString.length(); ++i)

Use of != isn't recommended, as one could go past the end of something. A for loop ends when the end condition becomes false.

To lower a string, works for ASCII:

http://stackoverflow.com/questions/313970/how-to-convert-stdstring-to-lower-case


Reverse:

http://www.cplusplus.com/reference/algorithm/reverse/


THe STL has algorithms, you can read about them in the reference top left of this page. You should be able to find the ones you need in there.

When passing a container, pass it by reference. Also, any arguments that you don't want to change in the body of the function should be const

std::string makeLower(const std::string& theString) {

Also, avoid using namespace std; put std:: before each std thing - It's the best way, and that's what the experts do. Google it to see why.

Good work for your code so far though :+)

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
// make string to lower...
std::string make_lower( std::string the_string ) {

    // http://www.stroustrup.com/C++11FAQ.html#for
    for( char& c : the_string ) c = std::tolower(c) ;
    return the_string ;
}

// reverse
std::string make_reverse( std::string str ) {

    // http://en.cppreference.com/w/cpp/string/basic_string/rbegin
    // http://www.stroustrup.com/C++11FAQ.html#uniform-init
    return { str.rbegin(), str.rend() } ;
}

//If String contains char.
bool str_contains( std::string str, char let ) {

    return str.find(let) != std::string::npos ;
}

// remove at index
std::string remove_at_index( std::string str, std::size_t index ) {

    if( index < str.size() ) /*** validate ***/ str.erase( index, 1 ) ;
    return str ;

}
Topic archived. No new replies allowed.