Segmentation Fault.

I am not entirely sure how to go about working on segmentation faults, I am aware of what they are but I am not seeing this issue in my program.

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>
using namespace std;


int main()
{
        bool word = false;
        int count = 0;
        char ch;
        char low;
        int array[26];
        char sent[100] = "Mary had a little lamb";
        int i;

        for (i=0; i<26; i++)
            array[i] = 0;

        for (int j=0; j<100; j++)
        {
        ch = sent[j];
        low = tolower( ch);
        array[low - int('a') ]++;
        }

        for (int k; k<26; k++)
        {
            if(array[k] != 0)
                cout << array[k] << " " << char(k + 'a') << endl;
        }
}
What if sent[j] is not a standard A-Z character, but instead it's a space like in your example?

Consider checking, between line 22 and 23, if low is in the range a-z.
I only get the segmentation fault from the bottom for loop... is that caused by the for loop you were talking about?
Oh well, for that, try initializing "k" to 0.
You probably didn't notice it.
Topic archived. No new replies allowed.