小编Kah*_*ahn的帖子

C - If/Else和Pointers返回错误的字符串

我们有一个函数最长,它返回由字母组成的最长子字符串.例:

longest("112****hel 5454lllllo454")
Run Code Online (Sandbox Code Playgroud)

会回来:lllllo

但是,当我运行程序时,它似乎返回lllllo454.这是功能:

char *longest(char *s){
    char *pMax = NULL;
    int nMax = 0;
    char *p = NULL;
    int n = 0;
    int inside = 0; //flag
    while(*s!='\0'){
        char c = *s;
        if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')){
            if(inside == 0){
                n = 1;
                p = s;
                inside = 1;
            }
            else
                n++;
            if(inside == 1){
                if(n > nMax){
                    nMax = n;
                    pMax = …
Run Code Online (Sandbox Code Playgroud)

c string pointers

1
推荐指数
1
解决办法
461
查看次数

标签 统计

c ×1

pointers ×1

string ×1