2016-09-27 72 views
1

我需要爲學校做這個程序,但是由於「分段錯誤」(11),我得到錯誤項目終止。我相信這與程序中缺乏內存有關,但是我並沒有真正看到內存被大量使用的功能。任何幫助,將不勝感激。學校項目分段錯​​誤

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

#define _CRT_SECURE_NO_WARNINGS 

int STRLEN = 36; 



int findOcurrance(char str[], char Char1){ 
for(int i = 0; i < STRLEN * 3; i++){ 
    if(str[i] == Char1) 
     return i; 
} 
return -1; 
} 

void replaceVowelsA(char str[], char Char) { 
for(int i = 0; i < STRLEN * 3; i++) { 
    int index = findOcurrance(str, Char); 
    str[index] = 'a';  
} 
} 

void insertaChar(char str[], char Char1, int index){ 
for(int i = STRLEN * 3; i >= index; i--){ 
    if(i != 0 && (str[i] != '\0' || i == strlen(str))){ 
     str[i] = str[i - 1]; 
    } 
    else if(i == 0){ 
     str[0] = ' '; 
    } 
} 
str[index] = Char1; 
} 

void adday(char str[]) { 
char consonants[42] = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z'}; 
for(int i = 0; i < STRLEN * 3; i++){ 
    if(i==0){ 
     for(int c = 0; c < 42; c++){ 
      if(str[i] == consonants[c]){ 
       insertaChar(str, 'y', i); 
       insertaChar(str, 'a', i); 
       break; 
      } 
     } 
    } 
    else if(i != 0){ 
     if(str[i-1] == ' '){ 
      for(int c = 0; c < 42; c++){ 
       if(str[i] == consonants[c]){ 
        insertaChar(str, 'y', i); 
        insertaChar(str, 'a', i); 
        break; 
       } 
      } 
     } 
    } 
} 
} 

int findWords(char str[]){ 
int count = 0; 
for(int i = 0; i < STRLEN * 3; i++){ 
    char c = str[i]; 
    if(isspace(c)) 
     count++; 
    if(count == 2) 
     return i + 1; 
} 
return -1; 
} 

char* stringReorder(char str[], int i){ 
if(i == -1){ 
    return str; 
} 
else{ 
    char string1[STRLEN]; 
    char string2[STRLEN * 2]; 
    strncpy(string1, str, i); 
    strncpy(string2, &str[i], strlen(str) - i); 
    char string[STRLEN * 3]; 
    strcpy(str, string2); 
    strcat(str, " "); 
    strcat(str, string1); 
    return str; 
} 
} 

void main(void) { 
char mystring[STRLEN * 3]; 
printf("** Welcome to the Double Dutch game **\n"); 
printf("Please enter a string: "); 
scanf("%[^\n]s", mystring); 
char vowel = 'e'; 
replaceVowelsA(mystring, vowel); 
vowel = 'i'; 
replaceVowelsA(mystring, vowel); 
vowel = 'u'; 
replaceVowelsA(mystring, vowel); 
vowel = 'o'; 
replaceVowelsA(mystring, vowel); 
adday(mystring); 
int index = findWords(mystring); 
strcpy(mystring, stringReorder(mystring, index)); 
printf("Double Dutch translation: %s", mystring); 
} 
+0

首先,你似乎無處檢查字符串終止符。 –

+6

學習如何使用調試器比學習如何編寫代碼更重要。現在是學習前一技能的時候了。 – 2016-09-27 12:25:15

+0

'strncpy()'儘管名稱不是**字符串**函數:結果「字符串」在所有情況下可能不是零終止的。你不能使用結果數組來獲得正確的字符串函數(比如'strcat()')。 – pmg

回答

4

在功能replaceVowelsA(),你以特定字符替換字符串輸入你。在函數replaceVowelsA()中,使用findOcurrace()來查找是否存在Char。如果不是,它將返回-1,但你沒有檢查返回值,所以它可能返回-1,在代碼行22中,它可能執行代碼str[-1] = 'a',這是非常危險的行爲。

您的字符串操作還有其他問題,例如您沒有檢查的終止字符NULL

如果您使用GNU工具鏈,您可以添加-g選項。您也可以使用其他調試方法來查找問題。

+0

謝謝! :)這至少解決了分割問題,添加了if語句以防止它執行,程序仍然不能正常運行,但是我希望我能夠以某種方式弄清楚 –