从C中的用户输入中搜索字符串数组

时间:2018-07-23 02:39:38

标签: c

我已经完成了这项作业,让用户输入他们想要的字符串行数,然后他们继续输入并存储在2D数组中(从而创建字符串数组)。然后将显示一个开关案例菜单,该菜单应该

  1. 搜索用户输入的字符,返回该字符在数组中出现的次数

  2. 搜索用户输入的单词,返回单词在数组中出现的次数

  3. 让用户输入指定的单词长度并返回指定长度的单词出现的次数。

我的代码有几个问题。程序运行时没有编译器错误。 searchByCharacter函数可以正常工作,但是searchByWord仅返回0值,而不管输入的任何单词,在我为searchByLength函数输入数字后什么也没有发生。一旦选择searchByLength函数,输入长度后程序将冻结。我已经有一段时间了,我不知道我要去哪里错了。

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

#define MAX_LINE_LENGTH 80
#define MAX_LINES 10
#define WORD_LENGTH 20

void readText(char text[][MAX_LINE_LENGTH], int n)
{
    int i;
    printf("Enter %d number of lines:\n", n);
    for(i = 0; i < n; i++)
    {
        scanf(" %[^\n]s", text[i]);
    }
}

int searchByCharacter(char text[][MAX_LINE_LENGTH], int n, char c)
{
    int i, charCount = 0, j = 0;
    for(i = 0; i < n; i++)
    {
        j = 0;
        while(text[i][j] != '\0')
        {
            if(text[i][j] == c)
            {
                charCount++;
            }
            j++;
        }
    }
    return charCount;
}

int searchByWord(char text[][MAX_LINE_LENGTH], int n, char * keyword)
{
    int i, wordCount = 0;
    for(i = 0; i < n; i++)
    {
        int j = 0;
        int lengthOfWord = 0;
        char wordCheck[WORD_LENGTH];
        char * currentLine = text[i];
        while(currentLine[j] != '\0')
        {
            if (currentLine[j] == ' ' || currentLine[j] == '\n' || currentLine[j] == ',' || currentLine[j] == '.' ||
                currentLine[j] == ';')
            {
                wordCheck[lengthOfWord] = '\0';
                int matchingWord = strcmp(wordCheck, keyword);
                if(matchingWord == 0)
                {
                    wordCount++;
                }
                lengthOfWord = 0;
                j++;
                continue;
            }
            wordCheck[lengthOfWord] = currentLine[n];
            lengthOfWord++;
            j++;
        }
    }
    return wordCount;
}

int searchByLength(char text[][MAX_LINE_LENGTH], int n, int wordLen)
{
    int i, lengthCount = 0;
    for(i = 0; i < n; i++)
    {
        int lengthOfWord = 0;
        int j = 0;
        char * currentLine2  = text[i];

        while(currentLine2[j] != '\0')
        {
            if (currentLine2[j] == ' ' || currentLine2[j] == '\n' || currentLine2[j] == ',' || currentLine2[j] == '.' ||
                currentLine2[j] == ';')
            {
                    if(lengthOfWord == wordLen)
                    {
                        lengthCount++;
                    }

                    lengthOfWord = 0;
                    n++;
                    continue;
            }
            lengthOfWord++;
            n++;
        }
    }
    return lengthCount;
}

int main(void)
{
    char textInput[MAX_LINES][MAX_LINE_LENGTH];

    printf("Enter number of lines (<10): ");
    int textLines = 0;
    scanf("%d", &textLines);

    while(textLines < 1 || textLines > 10)
    {
        printf("Invalid Input.\n");
        printf("Enter number of lines (<10): ");
        scanf("%d", &textLines);
    }
    if(textLines >= 1 && textLines <= 10)
    {
        readText(textInput, textLines);

        int menuActive = 1;

        while(menuActive)
        {

            printf("\nText Analysis\n----\n");
            printf("1-Search By Character\n2-Search By Word\n3-Search By Length\n0-Quit\nPlease enter a selection: ");

            int selection;
            scanf("%d", &selection);

            switch(selection)
            {
                case 0:
                    menuActive = 0;
                    break;
                case 1:
                    printf("Selected 1\n");

                    printf("Enter a character to search: ");
                    char characterSearch;
                    scanf(" %c", &characterSearch);

                    int characterwordCount = searchByCharacter(textInput, textLines, characterSearch);
                    printf("\nNumber of occurence of %c = %d", characterSearch, characterwordCount); 
                    break;
                case 2:
                    printf("Selected 2\n");

                    printf("Enter a word to search: ");
                    char wordSearch[MAX_LINE_LENGTH];
                    scanf(" %s", wordSearch);

                    int lengthwordCount = searchByWord(textInput, textLines, wordSearch);
                    printf("\nNumber of occurence of %s = %d", wordSearch, lengthwordCount);
                    break;
                case 3:
                    printf("Selected 3\n");

                    printf("Enter search length: ");
                    int wordLength;
                    scanf(" %d", &wordLength);

                    int wordLengthwordCount = searchByLength(textInput, textLines, wordLength);
                    printf("Number of words with length %d = %d", wordLength, wordLengthwordCount);
                    break;
                default:
                    printf("Invalid Input.\n");
            }
        }
    printf("You Have Quit!\n");
    }

    return 0;
}

0 个答案:

没有答案