【问题标题】:what's wrong with my code, it doesn't print the third sentence我的代码出了什么问题,它不打印第三句
【发布时间】:2022-10-14 16:51:56
【问题描述】:

编写一个允许用户输入句子的程序。然后程序应该打印从第三个单词开始的句子。

例如它应该像这样打印:

输入一句话:欢迎来到编程课程 编程课程

我的代码的问题是它只打印以第二个单词而不是第三个单词开头的句子。我想知道我的代码有什么问题?

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

#define SIZE 100

int main(void)
{

    char arr[SIZE];
    char* p = NULL;


   

        int count = 0;
        printf("Enter a sentence:");
        fgets(arr, SIZE, stdin);
        for (int i = 0; i < SIZE; i++) {
            if (arr[i] == ' ') {
                count++;

            }
        }
        if (count < 3 ) {
            printf("The sentence is to short!\n");
        }
        else {
            count = 0;
            for (int i = 0; i < strlen(arr); i++) {
                if (arr[i] == ' ') {
                    count++;
                }
                if (count == 2) {
                    p = &arr[i + 1];
                }
                
            }printf("%s\n", p);
            
        }
        return 0;
   
}

【问题讨论】:

  • for (int i = 0; i &lt; SIZE; i++) 这太重要了。在此循环中也使用 strlen
  • @badprogrammer 要么将您的解决方案发布为答案,要么删除问题。

标签: c for-loop split whitespace c-strings


【解决方案1】:

我不知道为什么程序从第二个单词而不是第三个单词输出句子,但无论如何这个 for 循环

        for (int i = 0; i < strlen(arr); i++) {
            if (arr[i] == ' ') {
                count++;
            }
            if (count == 2) {
                p = &arr[i + 1];
            }
            
        }

不正确,因为当 count 等于 2 时,对于字符串中不是空格的每个字符,p 的值会因为这个 if 语句而改变

            if (count == 2) {
                p = &arr[i + 1];
            }

至少你需要插入一个像这样的break语句

            if (count == 2) {
                p = &arr[i + 1];
                break;
            }

也是第一个 for 循环

    for (int i = 0; i < SIZE; i++) {
        if (arr[i] == ' ') {
            count++;

        }
    }

调用未定义的行为,因为您需要使用 strlen(arr) 而不是 SIZE。

此外,您需要在计算字数之前从字符串中删除尾随的换行符' '

除此之外,这种方法在任何情况下都是不正确的,因为当字符串包含相邻空格时它不起作用。

【讨论】:

  • 谢谢来自莫斯科的弗拉德
  • 我不想争论,如何测试未初始化但有效的字节等于''(SP)调用UB? (当然,它可能会忽略计数,但这仍然可以通过代码中的其他补救措施进行管理。)
  • @Fe2O3 count 的值将是未定义的。
【解决方案2】:

你试图用太多做太多。简化代码使生活更轻松。

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

#define SIZE 100

int main(void) {
    printf( "Enter a sentence:" );

    char arr[SIZE] = { 0 }; // define (and initialise) variables close to use
    fgets( arr, SIZE, stdin );

    int i = 0;
    while( arr[i] &&  isspace( arr[i] ) ) i++; // skip over leading whitespace (not considered)
    while( arr[i] && !isspace( arr[i] ) ) i++; // skip over the 1st "word" (if present )
    while( arr[i] &&  isspace( arr[i] ) ) i++; // skip over intervening whitespace  (if present )
    while( arr[i] && !isspace( arr[i] ) ) i++; // skip over the 2nd "word" (if present )
    while( arr[i] &&  isspace( arr[i] ) ) i++; // skip over intervening whitespace  (if present )

    if( arr[i] )
        printf( "%s
", arr + i );
    else
        printf("The sentence is to short!
");

    return 0;
}
Enter a sentence:   Now    is   the   time for all good men
the   time for all good men

您可以看到冗余代码(测试空格,然后测试非空格。)这可以“打包”在一个只需要几个参数的函数中。我会把它留作练习。 (也许看看strpbrk()...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-09
    • 2013-03-06
    • 2016-02-27
    • 2017-02-08
    • 2013-05-12
    • 1970-01-01
    相关资源
    最近更新 更多