【问题标题】:Look for a specific character in a sentence and print each word that has that charcter查找句子中的特定字符并打印包含该字符的每个单词
【发布时间】:2015-04-10 19:15:46
【问题描述】:

我必须向用户询问一个句子和一个字符。然后,我必须找到该句子中具有该字符的每个单词并打印这些单词。我不能使用 strtok() 这是我到目前为止所拥有的。它几乎可以工作,但无法正确打印单词。有人可以帮忙吗?对不起,我是 C 的新手。。

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

int main()
{
char character;
char sentence[] = "this is my first sentence";
char *strPtr;
char word;

//printf("Enter a sentence\n");
//fgets(sentence, 500, stdin);

printf("Enter a character\n");
scanf("%c", &character);


printf("Words containing %c are: \n", character);

strPtr = sentence;

while(*strPtr != '\0')
{
    if (*strPtr == character)
    {
        printf("%s\n", strPtr);
    }
    strPtr++;
}

return 0;
}

【问题讨论】:

  • 您需要记住找到的前一个空格字符的索引,并等待找到下一个(或字符串结尾),然后再提取包含所需字符的单词。想想如果您选择字母e 会发生什么。 sentence 这个词只能打印一个。
  • 是的,使用两个指针sp(起始指针)和strPtr。在每个空格的开头和之后将sp 设置为每个单词的开头。当strPtr 命中下一个空格时,设置null-terminator 并打印包含您的字符的单词。
  • 我不知道我将如何编码......在while循环中,我检查strPtr何时等于一个空格,然后我将该单词开头的值存储为sp。以及如何打印这个词?
  • 注意:代码不能改变文字,因为文字将在内存中标记为“只读”的页面上。因此我会合并两个指针的建议,但一次输出一个字符,直到遇到空格。更新两个指向下一个非空格字符的指针。在循环中执行所有操作,直到遇到 NUL 字符
  • @user3629249 不是这样:sentence[]文字初始化的。如果声明是char *sentence = "this is my first sentence";,您的评论将是正确的。

标签: c string pointers character


【解决方案1】:
#include <stdio.h>
#include <ctype.h>

int main(void){
    char character;
    char sentence[] = "this is my first sentence";
    char word[sizeof sentence];
    char *strPtr, *wordPtr = word;
    int contain = 0;

    printf("Enter a character\n");
    scanf("%c", &character);

    printf("Words containing %c are: \n", character);

    for(strPtr = sentence; *strPtr != '\0'; strPtr++){
        if(isspace(*strPtr)){
            *wordPtr = '\0';
            if(contain)
                printf("%s\n", word);
            //reset
            contain = 0;
            wordPtr = word;
        } else {
            if(*strPtr == character)
                contain = 1;//find
            *wordPtr++ = *strPtr;
        }
    }
    *wordPtr = '\0';
    if(contain)
        printf("%s\n", word);

    return 0;
}

【讨论】:

  • 它不打印最后一个单词。也需要在'\0' 上触发。
  • 我明白了,你在演示循环后检查contain
  • 我用e编译运行,没有输出。但是您在链接的演示中添加了更多代码。
  • 我已在您的评论之前添加。未附加编辑链接。;-)
【解决方案2】:

或者,没有ctype.h

#include <stdio.h>

int main ()
{
    char character;
    char sentence[] = "this is my first sentence";
    char *strPtr = sentence;
    char *sp = sentence;

    //printf("Enter a sentence\n");
    //fgets(sentence, 500, stdin);

    printf ("\nCurrent string: %s\n\n", sentence);
    printf ("Enter a character: ");
    scanf ("%c", &character);

    printf ("\nWords containing %c are:\n\n", character);

    while (*strPtr) {                                   /* for each character   */
        if (*strPtr == character) {                     /* if match char        */
            while (*strPtr &&*strPtr != ' ') strPtr++;  /* find next space      */
            *strPtr = 0;                                /* null-terminate       */
            printf ("    %s\n", sp);                    /* print word           */
            *strPtr = ' ';                              /* replace w/original   */
            while (*strPtr && *strPtr == ' ') strPtr++; /* find next non-space  */
            sp = strPtr;                                /* set start pointer    */
        }
        else if (*strPtr == ' ')                        /* if no match & ' '    */
        {
            while (*strPtr && *strPtr == ' ') strPtr++; /* find next non-space  */
            if (*strPtr) sp = strPtr;                   /* set start pointer    */
        }
        else
            strPtr++;                                   /* advance pointer      */
    }

    return 0;
}

输出

$ ./bin/nostrtok

Current string: this is my first sentence

Enter a character: s

Words containing s are:

    this
    is
    first
    sentence

$ ./bin/nostrtok

Current string: this is my first sentence

Enter a character: e

Words containing e are:

    sentence

$ ./bin/nostrtok

Current string: this is my first sentence

Enter a character: x

Words containing x are:

【讨论】:

  • 无法更改文字(如用 NUL 替换字符)。建议一次打印一个字符,直到下一个空格或 NUL。
  • 它不是 literal 它是 字符数组... 文字是 char *sentence = "this is my first sentence";
猜你喜欢
  • 2018-05-06
  • 1970-01-01
  • 2016-04-07
  • 1970-01-01
  • 2016-05-05
  • 2014-10-27
  • 1970-01-01
  • 1970-01-01
  • 2013-10-09
相关资源
最近更新 更多