在句子中查找特定字符并打印具有该字符的每个单词

时间:2015-04-10 19:15:47

标签: c string pointers character

我必须向用户询问一个句子和一个角色。然后,我必须找到在该句子中具有该字符的每个单词并打印这些单词。我不能用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;
}

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

或者,没有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: