Segmentation fault 11 in code (Beginner)

I want to see if I can flip the words in a sentence like the one below (Instead of "HELLO GOOD SIR HOW ARE YOU " it should be "YOU ARE HOW SIR GOOD HELLO"), but after I compile the program it gives me segmentation fault 11 :( . What's wrong??

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>

using namespace std;

int main()
{
	int a, j=0 ;
	string arr2[50], newtxt = "", oldtxt = "HELLO GOOD SIR HOW ARE YOU ";

	do {

		a = oldtxt.find(" ");

		if (a != -1){
			arr2[j] = oldtxt.substr(0,a);
			oldtxt.erase(0,a);}
		else
			arr2[j] = oldtxt;

		j++;

		} while (a != -1);

	for (int g = 50; g >= 0; g--){
		newtxt += arr2[j];
	}

	cout << newtxt << endl;

	return 0;
}

Array arr2 has no element with index 50. So this loop

1
2
3
	for (int g = 50; g >= 0; g--){
		newtxt += arr2[j];
	}


is invalid.
Thank you very much for your answer, vlad from moscow!!! Can you give me advice on how can I replace the array with something auto-resizable? Perhaps a vector?
Yes you can use std::vector or maybe in this case it is better to use std::list or even std::stack.:)
Last edited on
Use vector instead of a C-style array and use the reverse algorithm.

I'll provide 2 useful functions "delimit" and "undelimit" when dealing strings and you want to translate them into a vectors:


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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

//
//
//
//
//

#include <vector>
#include <algorithm>
#include <iostream>
#include <string>

typedef unsigned int uint32;
// ============================================================================
//                                                      Delimit Line
std::vector<std::string> delimit(const std::string& line,
                                 const std::string& delimiter = " ,\t")
{
    std::vector<std::string> newdelim;
    size_t d1 = 0;
    size_t d2 = 0;
    while(true) {
        d1 = line.find_first_not_of(delimiter, d2);
        if(d1 == std::string::npos) { break; }
        d2 = line.find_first_of(delimiter, d1);
        std::string delim = "";
        try {
            delim = line.substr(d1,d2-d1);
        } catch (std::exception& e) {
            std::cout << "Standard Exception : " << e.what() << std::endl;
        }
        if(!delim.empty()) { newdelim.push_back(delim); }
    }

    return newdelim;
}

// ============================================================================
//                                                      Undelimit Line
std::string undelimit(std::vector<std::string> delimStr, std::string delimiter = " ")
{
    std::string str = "";
    for(uint32 i = 0; i < delimStr.size(); ++i) {
        str += delimStr.at(i);
        if(i < delimStr.size()-1) { str += delimiter; }
    }
    return str;
}

// ============================================================================
//                                                      main
int main()
{
    std::string str = "HELLO GOOD SIR HOW ARE YOU";
    std::vector<std::string> oldstr = delimit(str);
    std::vector<std::string> newstr = oldstr;

    std::reverse(newstr.begin(), newstr.end());

    std::cout << std::endl;
    std::cout << "\tOLD STRING : " << undelimit(oldstr) << std::endl;
    std::cout << "\tNEW STRING : " << undelimit(newstr) << std::endl;
    std::cout << std::endl;

    return 0;
}


Output:



OLD STRING : HELLO GOOD SIR HOW ARE YOU
NEW STRING : YOU ARE HOW SIR GOOD HELLO


C++ streams offer some built-in tokenizing abilities, you could leverage those to make it simpler:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <iterator>

int main()
{
    std::string oldtxt = "HELLO GOOD SIR HOW ARE YOU ";
    std::istringstream buf(oldtxt);
    std::istream_iterator<std::string> beg(buf), end;
    std::vector<std::string> v(beg, end);
    std::copy(v.rbegin(), v.rend(), std::ostream_iterator<std::string>(std::cout, " "));
    std::cout << '\n';
}

online demo: http://ideone.com/Kg7Zgc
Topic archived. No new replies allowed.