Thread: segmentation fault in program that converts names to upperr initials only

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    180

    segmentation fault in program that converts names to upperr initials only

    This program isa supposed to turn input like Cristiano Ronaldo into CR, TGV into T and alex martin into AM.
    I have the following code. What i wanted to do was get the first letter and then ignore all letter intul nun letter appears. then jump it if its space and continue process or quit if its new line or '\0'.
    However the following code is giving a pretty nice segmentation fault and i can't seem to figure out why.


    Code:
    #include <stdio.h>#include <ctype.h>
    
    
    #define NAME_LEN 80+1
    
    
    int main()
    {
        char userName[NAME_LEN], newArray[NAME_LEN];
        int pos = -1;
        printf("Enter name: ");
        fgets(userName, NAME_LEN, stdin);
        printf("%s", userName);
        for(int i = 0; userName[i] != '\n' || userName[i] != '\0'; i++)
        {
            
            if (userName[i] >= 'a' && userName[i] <= 'z')
            {
                newArray[++pos] = userName[i] - ('a' - 'A');
            } 
            else
            {
                newArray[++pos] = userName[i];
            }
            while (!isblank(userName[i]) || userName[i] != '\n')
            {
                i++;
            }
        }
        userName[++pos] = '\0';
        printf("%s", userName);
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Code:
    userName[i] != '\n' || userName[i] != '\0';
    This will always evaluate to true. You want to use && instead of ||.

    Code:
    !isblank(userName[i]) || userName[i] != '\n'
    Same here. Plus you're not checking for the null character, so you might run off the end of the string.

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    180
    Thanks. I have to pay more attention in those parts. I don't have any more segmentation fault but output is still not correct, it's printing some weird characters after the correct ones. Like barack obama gives this:

    segmentation fault in program that converts names to upperr initials only-8e3d6cddcb-png

    Code:
    #include <stdio.h>#include <ctype.h>
     
     
    #define NAME_LEN 80+1
     
     
    int main()
    {
        char userName[NAME_LEN], newArray[NAME_LEN];
        int pos = -1;
        printf("Enter name: ");
        fgets(userName, NAME_LEN, stdin);
        printf("%s", userName);
        for(int i = 0; userName[i] != '\n' && userName[i] != '\0'; i++)
        {
             printf("early in for loop\n");
             printf("i = %d\n", i);
            if (userName[i] >= 'a' && userName[i] <= 'z')
            {
                printf("got here.\n");
            
                pos++;
                newArray[pos] = userName[i] - ('a' - 'A');
                printf("%s", newArray);
            } 
            else
            {
                pos++;
                newArray[pos] = userName[i];
                
            }
            while (!isblank(userName[i]) && userName[i] != '\n')
            {
                printf("is blank(userName[i]  =%d\n, userName[i] = %c\n)", isblank(userName[i]), userName[i]);
                i++;
            }
        }
        pos++;
        userName[pos] = '\0';
        printf("%s", newArray);
    }
    Sorry for the prints, iam still in the process of learning how to use gdb, it is a bit intimidating as of now .

  4. #4
    Registered User
    Join Date
    Apr 2015
    Posts
    180
    Got it... Sorry if anyone was in the process of finding bug for me, it was in second last line i was assigning '\0' to the original input from user not the one where i was inserting the initials so i suppose it was printing some junk (but i confess i would expect it to go more crazy on me, why did only print 2 random chars?)

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by telmo_d View Post
    ... why did only print 2 random chars?)
    When "printf()" prints a string, it continues printing characters until it sees the null character '\0'.

    So if you forget to terminate the string, "printf()" will keep going until it reaches a character that happens to have the value of '\0'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault in my program
    By Dr.JacKaL in forum C Programming
    Replies: 4
    Last Post: 10-29-2014, 02:31 PM
  2. Simple program has segmentation fault??
    By fsi in forum C Programming
    Replies: 4
    Last Post: 12-04-2011, 10:25 AM
  3. Segmentation fault on my program
    By blackswan in forum C Programming
    Replies: 2
    Last Post: 05-11-2005, 04:47 PM
  4. Segmentation fault in beginning or program
    By tameeyore in forum C Programming
    Replies: 1
    Last Post: 02-26-2005, 08:16 PM
  5. Segmentation fault !, program works:S
    By jspri in forum C Programming
    Replies: 8
    Last Post: 09-28-2004, 05:25 PM