字符串 C 上的分段错误



我正在尝试编写代码以获取学生姓名,姓氏和ID,然后以稳定格式打印。示例:(输入姓名:"Alp David Oflas"输入 ID:"20110702037",然后输出应类似于"OFLAS,Alp David,于 2011 年作为第 37 名学生加入工程系。所以我正在尝试使用这段代码,除了 ID 部分,但在 repl.it 我收到"以非零状态退出"错误,当我在 Ubuntu 上尝试时,我得到"分段错误(核心转储("。所以要到4个小时才能解决这个问题。你能帮我哪一部分代码导致该错误吗?

注意:我不能使用任何其他功能。我必须通过循环和比较来做到这一点。

#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]!="";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]="";
//Previous surname just be like "salfO" so need to convert it// 
    char newsurname[100]; 
    int surnameL;
    for(surnameL=0;surname[surnameL] !="";surnameL++);
    int k=0;
    while (surname[k] != ""){
        newsurname[k]=surname[surnameL];
        k++;
        surnameL--;
    }
    newsurname[k] = "";
//Just capitalizing surname for dependant output//  
    int x=0;
    while (newsurname[x] !=""){
        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;
}

已解决...""问题,必须''.

相关内容

  • 没有找到相关文章

最新更新