0
Follow
0
View

What's wrong with experiment 9, 3161

duar1977 注册会员
2023-02-28 19:32

The above code is incorrect, modify the answer with the code as follows:

First uses scanf() to get the string, which ends at the first whitespace character, so you enter a string with a space. Getting the input is an error, and you can use gets() instead;

This code makes an error judgment if the last word is followed by a space. It also makes an error judgment if it is a non-alphabetic character. Increase the error judgment when calculating the length from back to back, start with the first alphabetic character and end with the first non-alphabetic character.
https://www.runoob.com/cprogramming/c-function-isalpha.html
https://baike.baidu.com/item/gets/787649?fr=aladdin
https://baike.baidu.com/item/scanf/10773316?fr=aladdin

  #include 
 #include 
int ln(char *s)
{
    int i=0;
    char * oriS=s;
    while(*s!='\0') // 让指针指向字符串最后的空字符 
        s++;
    s--;  // 让指针指向空字符前一个字符位置 
    //printf("before,s=%s\n",s);
    
    // https://www.runoob.com/cprogramming/c-function-isalpha.html
    while(isalpha(*s)==0&&s>=oriS)  // 如果当前字符不是字母并且仍在字符串内,则指针往前移动一个位置,直到遇到第一个字母 
    {
        s--;
    }
    
    while(isalpha(*s)!=0&&s>=oriS){  // 如果当前字符是字母,且仍在字符串内,则进行单词长度计算。直到遇到第一个非字母 
        i++;
        s--;
    }
    
   // printf("end,s=%d\n",s); 
    return i;
}

int main()
{
    char a[1024];
    // https://baike.baidu.com/item/scanf/10773316?fr=aladdin
   // scanf("%s",a);
   // https://baike.baidu.com/item/gets/787649?fr=aladdin
       gets(a);
    //printf("a=%s\n",a);
    
    int l=ln(a);
    printf("%d\n",l);
    return 0;
}

 

img

yanxuefeng1516 注册会员
2023-02-28 19:32

This code makes an error judgment if the last word is followed by a space. It also makes an error judgment if it is not a letter character, increasing the error judgment when calculating the length from back to front by starting with the first letter character.

 #include 
 #include 
int ln(char *s)
{
    int i=0;
    while(*s!='\0')
        s++;
    s--;
    //printf("before,s=%s\n",s);
    
    // https://www.runoob.com/cprogramming/c-function-isalpha.html
    while(isalpha(*s)!=0)
    {
        s--;
    }
    
    while(*s!=' '){
        i++;
        s--;
    }
    
   // printf("end,s=%d\n",s); 
    return i;
}

int main()
{
    char a[1024];
   // scanf("%s",a);
   // https://baike.baidu.com/item/gets/787649?fr=aladdin
       gets(a);
    //printf("a=%s\n",a);
    
    int l=ln(a);
   // printf("%d\n",l);
    return 0;
}

 

dutian114 注册会员
2023-02-28 19:32
< div class = "md_content_show e397 data - v - 3967" = "" >

thank you

About the Author

Question Info

Publish Time
2023-02-28 19:32
Update Time
2023-02-28 19:32

Related Question

How to access the H2 console for Spring Boot without Spring Web?

当使用form_with ruby on rails时,语法错误,意外的tIDENTIFIER

Linq查询select new with List

当替换dataframe中的字符串时,在pandas中设置withcopywarning

Dataframe withcolumn

How to use storybook with real data?

Spark withcolumnrename不会在for循环中工作

Commit must be signed with a GPG key - After merge/rebase

将str.starts_with()与来自另一列的值进行极化

AWS AppSync unauthenticated access WITHOUT Amplify