使用strcpy在特定字符后复制字符串的一部分

时间:2019-03-29 18:08:54

标签: c string pointers c-strings strcpy

所以我让用户输入目的地,飞行时间和停留时间。输入将始终看起来像“目的地,0:00、0:00”,我正在尝试复制输入并重新打印。我有一些功能可以确保以正确的格式输入它,但是有没有办法打印出第一个逗号之后的飞行时间呢?也许我已经使它复杂化了,任何帮助都会很棒!谢谢!

当前目标:验证用户输入正确格式的字符串后,我想复制它,然后重新打印。我不能使用sscanf,strtok或任何其他对字符串数据进行标记化的函数。

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

/* == FUNCTION PROTOTYPES == */
int checkForComma(char* pUserString, char comma);
int checkForColon(char* pUserString, char colon);


/* == CONSTANTS == */
const char kComma = ',';
const char kColon = ':';

int main()
{
    int numbers = 0;
    int loopEnd = 0;
    int flightTime = 0;
    int inputAccepted = 0;
    int commaFound = 0;
    int colonFound = 0;

    while (loopEnd != 1)
    {
        char programOutput[81] = "";
        char userInput[81] = "";

        fgets(userInput, 81, stdin);

        strcpy_s(programOutput, 81, userInput);

        if ((userInput == NULL) || (userInput == '\0'))
        {
            printf("Please enter valid input!\n");
            return 0;
        }

        if (userInput == '.')
        {
            loopEnd = 1;
        }

        while (inputAccepted == 0)
        {
            commaFound = checkForComma(programOutput, kComma);
            colonFound = checkForColon(programOutput, kColon);

            if (commaFound && colonFound == 2)
            {
                printf("Input Accepted!");

                while (commaFound && colonFound == 2)
                {
                    int numberCounter = 0;
                    char* numCheck = programOutput;


                }
                break;
            }
            else
            {
                printf("Input not accepted!");
                return 0;
            }
        }

        printf("Press ENTER key to Continue\n");
        getchar();
    }

    return 0;
}


int checkForComma(char* pUserString, char comma)
{
    int counter = 0;

    char* pCheck = pUserString;

    while ((pCheck = strchr(pCheck, comma)) !=NULL)
    {
        counter++;
        pCheck++;
    }

    return counter;
}

int checkForColon(char* pUserString, char colon)
{
    int counter = 0;

    char* pCheck = pUserString;

    while ((pCheck = strchr(pCheck, colon)) != NULL)
    {
        counter++;
        pCheck++;
    }

    return counter;
}

0 个答案:

没有答案