Thread: Segmentation Fault (Core Dumped)

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    1

    Unhappy Segmentation Fault (Core Dumped)

    Hi guys,

    I am writing a program that needs to do a few things with an input string:
    -Replace all lower case vowels (a, e, i, o, u) with the letter ‘a’.
    -Add to each word that starts with a consonant the letters “ay” at the front of this word.
    -Place the first two words at the end of the sentence.

    I have written the next code, but I keep getting a segmentation fault. I have tried to execute the functions one by one, but I haven't come very far with that. Could someone please help me?

    Code:
    #include <stdio.h>
    #include <string.h>
    #define lengthstr 32
    
    
    void replaceVowels(char str[]);
    void replaceChars(char str[], char sChar, char rChar);
    void expandString(char str[], char aChar[], int index);
    void addAY(char str[]);
    int findFO(char str[], char aChar);
    void reorderString(char str[], char index0, char index1);
    
    
    
    
    
    
    void main(void)
    {
        
     char myString[lengthstr];   
    
    
     printf("** Welcome to the Double Dutch game\n");
     printf("Please enter a string: ");
     scanf("%[^\n]s", myString);
     printf("%s\n", myString);
     replaceVowels(myString);
     addAY(myString);
     expandString(myString, " ", strlen(myString));
     
     int index0 = findFO(myString, ' ');
     myString[index0] = '+';
     
     int index1 = findFO(myString, ' ');
     myString[index1] = '+';
     
     reorderString(myString, index0, index1);
     
     replaceChars(myString, '+', ' ');
     
     printf("Double Dutch translation: %s\n", myString);
    }
    
    
    
    
    void replaceVowels(char str[])
    {
        int i;
        
        for(i = 0; str[i] != '\0'; i++)
        {
            if(str[i] == 'e'||str[i] == 'i'||str[i] == 'o'||str[i] == 'u')
            {
                str[i] = 'a';
            }
            
        }
        
    }
    
    
    void replaceChars(char str[], char sChar, char rChar)
    {
        int i = findFO(str, sChar);
        
        while(i >= 0)
        {
         str[i] = rChar;
         i = findFO(str, sChar);
        }
    }
    
    
    
    
    void expandString(char str[], char aChar[], int index)
    {
    
    
     char str2[30];
     
     strncpy(str2, str, index);
     
     int length = strlen(str2);
     
     length = length + strlen(aChar);
     strcpy(str2+length, str+index);
     
     strcpy(str, str2);
        
     }
     
     void addAY(char str[])
     {
         int i;
         
         if(str[0] != 'a'&&str[0] != 'e'&&str[0] != 'o'&str[0] != 'u'&&str[0] != 'I')
         {
             expandString(str, "ay", 0);
         }
         
         i = findFO(str, ' ');
         
         while(i >= 0)
         {
             str[i] = '+';
             
             if(str[i+1] != 'a'&&str[i+1] != 'e'&&str[i+1] != 'o'&str[i+1] != 'u'&&str[i+1] != 'I')
             {
               expandString(str, "ay", i+1);  
             }
             
             i = findFO(str, ' ');
        }
        
        replaceChars(str, '+', ' ');
     }
        
    
    
    int findFO(char str[], char aChar)
    {
        int p = 0;
        int i = 0;
        
        while(i < strlen(str))
        {
         if(str[i] == aChar)
         {
          p = 1;
          break;
         }
         i++;
        }
        
        if(p == 1) 
        {
            return i;
        }
        
        else 
        {
            return -1;
        }
        
    }
    
    
    void reorderString(char str[], char index0, char index1)
    {
        char string0[strlen(str)];
        char string1[strlen(str)];
        char string2[strlen(str)];
        
        strncpy(string0, str, index0);
        strncpy(string1, str+index0, index1-index0);
        strncpy(string2, str+index1, strlen(str)-index1);
        
        strcpy(str, string2);
        strcat(str, string0);
        strcat(str, string1);
        
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    A couple of things to look out for.
    1. char string1[strlen(str)];
    This doesn't allocate enough space to copy str. You need to add 1 for the \0 as well.
    2. Strncpy doesn't automatically append a \0, so these strings might be longer than you think.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Read: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

    Something is wrong here:

    Code:
    if(str[0] != 'a'&&str[0] != 'e'&&str[0] != 'o'&str[0] != 'u'&&str[0] != 'I')

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault (core dumped)
    By johnmerlino in forum C Programming
    Replies: 2
    Last Post: 01-01-2014, 06:08 PM
  2. Segmentation fault (core dumped)
    By mrbotts in forum C Programming
    Replies: 2
    Last Post: 01-10-2012, 11:06 AM
  3. Segmentation Fault (core dumped)
    By sameer2904 in forum C Programming
    Replies: 3
    Last Post: 01-09-2012, 07:37 AM
  4. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 03:50 PM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 03:24 AM

Tags for this Thread