Trouble with string arrays

Pages: 12
This is for an assignment. How do I split up the first array (names) into the two other strings (last, first) without initializing the latter two?

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
#include <iostream>
#include <string>

using namespace std;



string names[6] = {
	"fuzz butt",
	"jerkus mclurkus",
	"acidface boy",
	"banana hands",
	"crawl space",
	"praying ghost"
};

static const size_t npos = -1;
string last  (size_t pos = ' ', size_t len = npos);
string first (size_t pos = 0, size_t len = ' ');


int main(int argc, char** argv) {
		
	for(int i=0;i<6;i++) {
			cout << names[i] << "\n";	
	}
	
	for(int a=0;a<6;a++) {		
			cout << last << ", " << first << "\n";
	}
		
}

Last edited on
First, what is the purpose of lines 17-19?

The names is an array of 6 objects, and each of them seem to contain two "words" separated by whitespace.

If 'last' is one object, what should it contain? One word? Concatenation of several words?
Splits up the names in the array in half.

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
#include <iostream>
#include <string>

int main()
{
    std::string names[6] =
    {
        "fuzz butt",
    	"jerkus mclurkus",
    	"acidface boy",
    	"banana hands",
    	"crawl space",
    	"praying ghost"
    };
    
    constexpr unsigned short MID =
    (sizeof(names) / sizeof(std::string)) / 2;
    
    std::string first[MID], last[MID];
    
    for (std::string::size_type i = 0; i != MID; ++i)
        first[i] = names[i];

    for (std::string::size_type i = MID; i != MID * 2; ++i)
        last [i - MID] = names[i];
        
    for (const std::string &s : names)
        std::cout << s << '\n';

    std::cout << "\n=======================\n\n";

    for (const std::string &s : first)
        std::cout << s << '\n';

    std::cout << '\n';

    for (const std::string &s : last)
        std::cout << s << '\n';
    
    return 0;
}
fuzz butt
jerkus mclurkus
acidface boy
banana hands
crawl space
praying ghost

=======================

fuzz butt
jerkus mclurkus
acidface boy

banana hands
crawl space
praying ghost
Last edited on
I truly don't know what those lines are meant to do. It was something I found on the web that sounded somewhat similar to what my aims are. I'm trying to get "first" to contain the first names and "last" to contain the last names without initializing. My professor hasn't really taught us what to do and has just told us to google it. I've stared at this code and dozens of forum pages and tutorials for about 12 hours now and I'm no closer to understanding what to do. I know it shouldn't be this hard but man...
Last edited on
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
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <sstream>

int main() {

    const std::size_t N = 6 ;
    const std::string names[N] = {
        "fuzz butt",
        "jerkus mclurkus",
        "acidface boy",
        "banana hands",
        "crawl space",
        "praying ghost"
    };

    {
        std::string first_names[N] ;
        std::string last_names[N] ;

        std::cout << "using std::string::find/rfind/substr\n----------------------\n" ;
        // http://en.cppreference.com/w/cpp/string/basic_string/find
        // http://en.cppreference.com/w/cpp/string/basic_string/substr
        // http://en.cppreference.com/w/cpp/string/basic_string/rfind

        for( std::size_t i = 0 ; i < N ; ++i ) {

            // assumes that names[i] contains at least one space
            first_names[i] = names[i].substr( 0, names[i].find( ' ' ) ) ; // part up to first space
            last_names[i] = names[i].substr( names[i].rfind( ' ' ) + 1 ) ; // part after last space
            std::cout << last_names[i] << ", " << first_names[i] << '\n' ;
        }
    }

    {
        std::string first_names[N] ;
        std::string last_names[N] ;

        std::cout << "\nusing std::istringstream\n----------------------\n" ;
        // http://en.cppreference.com/w/cpp/io/basic_istringstream

        for( std::size_t i = 0 ; i < N ; ++i ) {

            std::istringstream stm( names[i] ) ;
            // assumes that names[i] contains exactly two space separated parts
            stm >> first_names[i] >> last_names[i] ; // formatted input: skips white space
            std::cout << last_names[i] << ", " << first_names[i] << '\n' ;
        }
    }
}

http://coliru.stacked-crooked.com/a/589f8b5672b20385
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
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

string names[6] = {
	"fuzz butt",
	"jerkus mclurkus",
	"acidface boy",
	"banana hands",
	"crawl space",
	"praying ghost"
};
int main()
{
    for(size_t i = 0; i < 6; i++)
    {
        istringstream stream(names[i]);
        while(stream)
        {
            string first{}, last{};
            stream >> first >> last;
            cout << first << " " << last << "\n";
        }
    }
}
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
#include <iostream>
#include <string>

int main()
{
    std::string names[6] =
    {
        "fuzz butt",
    	"jerkus mclurkus",
    	"acidface boy",
    	"banana hands",
    	"crawl space",
    	"praying ghost"
    };
    
    
    for (const std::string &s : names)
        std::cout << s << '\n';
    
    std::cout << '\n';
    
    for (const std::string &s : names)
        std::cout << s.substr(0, s.find(' ')) << ' ' << std::flush;
        
    std::cout << "\n\n";
    
    for (const std::string &s: names)
        std::cout << s.substr(s.find(' ') + 1) << ' ' << std::flush;
        
    std::cout << '\n';
	
	return 0;
}

fuzz butt
jerkus mclurkus
acidface boy
banana hands
crawl space
praying ghost

fuzz jerkus acidface banana crawl praying 

butt mclurkus boy hands space ghost 
Last edited on
closed account (48T7M4Gy)
Is your program supposed to do something like this?

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
#include <iostream>
#include <string>

int main(int argc, char** argv)
{
    
    std::string names[6] =
    {
        "fuzz butt",
        "jerkus mclurkus",
        "acidface boy",
        "banana hands",
        "crawl space",
        "praying ghost"
    };
    
    size_t array_size = sizeof(names)/sizeof(std::string);
    
    std::string first;
    std::string last;
    size_t npos = 0;
    
    for(size_t i = 0; i < array_size; i++)
    {
        npos = names[i].find(' ');
        
        first = names[i].substr(0, npos);
        last = names[i].substr(npos +1);
        
        std::cout << last << ", " << first << '\n';
    }
}

butt, fuzz
 mclurkus, jerkus
 boy, acidface
 hands, banana
 space, crawl
 ghost, praying
Program ended with exit code: 0
Last edited on
Wow! 3 solutions in 3 minutes, the OP is so spoilt!!!
closed account (48T7M4Gy)
Must be a slow day for helpers. Excellent choice in using stringstream.
Cheers, I learn from great teachers ;))

http://www.cplusplus.com/forum/general/203741/
@gunnerfunner

4 solutions, actually.
Last edited on
Thank you all for the help, truly appreciated. I'm going to study these and learn where I can. Have a great day/night, people!

Kemort definitely came the closest to what I was looking for. Thank you!
Last edited on
crawlspace wrote:
Kemort definitely came the closest to what I was looking for.

Closest to what you were looking for or what you understand the most? What you understand may or may not be the best way.
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

int main(int argc, char** argv)
{
    std::string names[] =
    {
        "fuzz butt",
        "jerkus mclurkus",
        "acidface boy",
        "banana hands",
        "crawl space",
        "praying ghost"
    };
    
    for( std::string s : names){ std::cout << s.substr(s.find(' ') +1) << ", " << s.substr(0, s.find(' ')) << '\n';}
}
Last edited on
1
2
3
4
5
// for( std::string s : names)
// { std::cout << s.substr(s.find(' ')) << ", " << s.substr(0, s.find(' ')) << '\n';}

for( std::string s : names )
{ std::cout << s.substr( s.find(' ') + 1 ) << ", " << s.substr( 0, s.find(' ') ) << '\n'; 
closed account (48T7M4Gy)
Well done. Fixed.

E&OE
Last edited on
If any of you guys would have looked at my code, you would of seen that I was the only one added 1 to s.find(' '), haha.
Last edited on
closed account (48T7M4Gy)
Well done. jlborges did too by the look of it, but nevertheless.
I did not notice that in JLBorges code just like you, haha.
Pages: 12