2017-04-12 93 views
-2

我正在嘗試做代碼來獲取學生姓名和ID,然後用穩定格式打印它。例如:(輸入名字和姓氏:「Alp David Oflas」輸入ID:「20110702037」,輸出結果應該像「OFLAS,Alp David,2011年以第37名學生的身份加入工程部門。」)所以我試圖用這個代碼,除ID部分,但在repl.it我gettin「退出非零狀態」錯誤,當我在Ubuntu上嘗試我gettin「分段錯誤(核心轉儲)」。所以直到4小時才能解決這個問題。你能幫我哪一部分代碼導致這個錯誤嗎?字符串上的分段錯誤C

注:我不能使用任何其他功能。我必須循環做比較。

#include <stdio.h> 

void output(char name[100],char ID[100]){ 

    char surname[100]; 
//Calculating name string's length//  
    int nameL; 
    for(nameL=0;name[nameL]!="\0";nameL++); 

    int i=nameL; 
    int j=0; 
    int flag; 
    flag = "FALSE"; 
//Geting surname from value with decreasing i. It could be something like "Alp David Oflas" so we need just surname// 
    while (flag="FALSE"){ 
     if (name[i] == " "){ 
      flag="TRUE"; 
     } 
     else{ 
      surname[j] = name[i]; 
      i--; 
      j++; 
      flag = "FALSE"; 
     } 
    } 
    surname[j]="\0"; 
//Previous surname just be like "salfO" so need to convert it// 
    char newsurname[100]; 
    int surnameL; 
    for(surnameL=0;surname[surnameL] !="\0";surnameL++); 
    int k=0; 

    while (surname[k] != "\0"){ 
     newsurname[k]=surname[surnameL]; 
     k++; 
     surnameL--; 
    } 
    newsurname[k] = "\0"; 

//Just capitalizing surname for dependant output// 
    int x=0; 
    while (newsurname[x] !="\0"){ 
     if (newsurname[x] >= "a" && newsurname[x] <= "z"){ 
      newsurname[x] = (newsurname[x] -32); 
     } 
     x++; 
    }   

    printf("%s",newsurname); 

} 

//Geting values from keyboard// 
int main(){ 

    char name[100]; 
    char ID[100]; 
    printf("Enter name and surname:\n"); 
    gets (name); 
    printf("Enter ID:\n"); 
    gets (ID); 
    output(name,ID); 
    return 0; 
} 
+0

調查每個警告。 –

+0

@DavidSchwartz喜歡(不要用「gets」) –

+1

'int flag; flag =「FALSE」;而(flag =「FALSE」)'看起來不正確。 – Pang

回答

0

解決問題...在"\0"它必須是'\0'