0

//when ever it calls the hasLast function with only one name entered as an input it crashes with a seg fault for the initials more problem

#include <stdio.h>
#include <string.h>
#include <cs50.h>
#include <ctype.h>

struct a{
    char h;
    int lenStop;    //functions can now return more than one value
    bool go;
};


struct a getFin(string name, int length, int i, bool w);
char getFirst(string name);
bool isMiddle(string name, int length, int w);
bool hasLast(string name, int length);


int main (){
    struct a item;
    item.go = false;
    string name = get_string();


    int length;

    length = strlen(name);  //# frequently used

    char initial[3] = {' ', ' ', ' '};

    initial[0] = getFirst(name);      //first initial

    if(hasLast(name, length)){
        item = getFin(name, length, 0, item.go);        //last initial
        initial[2] = item.h;



        if(isMiddle(name, length, item.lenStop)){       //checks for middle name
            item = getFin(name, length, item.lenStop, item.go);
            initial[1] = item.h;                                        //gets and outputs name if has middle initial

            printf("%c%c%c\n", initial[0], initial[1], initial[2]);
    }
        else{
            printf("%c%c\n", initial[0], initial[2]);}

    }

}

struct a getFin(string name, int length, int i, bool w){

    struct a h;
    h.lenStop = 0;
    bool a = true;

    while(isspace(name[length - i]) && w == true){  //skips space inbtween middle and last name able to reuse functions then
    i++;
    }

    while(a == true){

        if(isspace(name[length - i])){   //finds next space after name being searched
        a = false;
        }
        else{
            i++;
        }
    }

    h.lenStop += i;  //able to reuse this number later

    h.h = name[(length - i) + 1];    //finds letter next to space


    if(islower(h.h)){           //makes uppercase if neccescary
        h.h = toupper(h.h);
    }

    h.go = true;        //so we know its been used already once
    return h;  //returns all i (location of the space before last name) and the charecter to main and if this fuction has been used already to find last name
}



char getFirst(string name){
    int i = 0;
    char a;
    while(true){
        if(isalpha(name[i])){       //skips all spaces
            a = name[i];
            if(islower(a)){
                a = toupper(a);
                return a;
            }
            return a;       //returns first letter uppercase
        }
        else{
            i++;
        }
    }
}

bool isMiddle(string name, int length, int w){

    int firstSpace = 0;
    int i = 0;

    while(isspace(name[i])){
    i++;
    }


    firstSpace = i;  // finds the first space length

    while(isspace(name[length - w])){
        w++;
    }
    while(isalpha(name[length - w])){
        w++;
    } //finds space after second name from the left

    w--;
    if((length - w) == firstSpace){  //checks to see if its the same space
        return false;
    }
    return true;

}


bool hasLast(string name, int length){
    int g = 0;
    int h = 0;
    while(isspace(name[g])){
    g++;
    }                                       //find last space in first long space
    printf("%i", g);

    while(isalpha(name[length - h])){    //find first space location staring from the keft
        h++;
    }

    printf("\n%i", (length - h));

    if(g == (length - h)){
        return false;
    }
    return true;
}
3
  • Sounds like time for debug50 (starts around 12:30). Commented Jan 17, 2019 at 15:08
  • Have you narrowed down which line is causing the seg fault yet? That's the first place to start. This is also a good task to practice debugging!
    – Cliff B
    Commented Jan 18, 2019 at 1:56
  • Should get_string take at least one string parameter? This line string name = get_string() doesn't compile for me.
    – stensal
    Commented Jan 19, 2019 at 17:33

1 Answer 1

1
while(isspace(name[length - i]) && w == true) {
   ++i;
}

This loop is the culprit. when i increases, and length - i decreases. At some iterations length-i will become a negative value, and you get segfault.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .