首页 > 解决方案 > 我需要弄清楚如何从输入数组中跳过星期几

问题描述

我的以下输出是

Example: Saturday, July 8, 2017, 22:14:10
 Enter date separated by commas in following format:
Friday, july 8, 2012, 22:10:12
Month   : Friday
 Day     : july
 Year    : 8
 Hour    : 2012
 Minute  : 22
 Second  : 10

我需要星期五离开那里。我不知道如何防止它被读入数组。我使用 if (token[0] && strcmp(*check,token) != 0) 创建了一个名为 check 的数组,以防止它使用 if (token[0] && strcmp(*check,token) != 0) 将其读入数组。这仅适用于星期六字符数组中的第一个字符串。

#include <stdio.h>
#include <string.h>
//This code will take user input of date and use 2-d array to store strings using delimeter as well. It will list all
// the date in descending order seperating from "Month", "Day", "Year", "Hour", "Minute", "Second"
//"Saturday, July 8, 2017, 22:14:10"

int main(void) {
    char *check[]= {"Saturday", "Monday", "Tuesday", "Wednesday", "Thursday", "Sunday", "Friday"};
    char date[8][20]; // creates 2 day array
    char *order[] = {"Month", "Day", "Year", "Hour", "Minute", "Second" }; // creates order of time
    printf(" Example: Saturday, July 8, 2017, 22:14:10 \n Enter date separated by commas in following format: \n");
    char text[64]; // array to store string initially
    fgets(text,64,stdin); // reads strings

    char* delims = " ,:"; // this is the delimeter that will be helpful for seperating it to different tokens
    char* token = strtok(text,delims);
    char** label =  order;
    int r = 0;
    while (token){

        if (token[0] && strcmp(*check,token) != 0) {
            strcpy(date[r], token);

        printf("%-8s: %s\n ",*label,date[r]);
        label++;
        r++; }
        token = strtok(NULL,delims); // this is so that it will find null values at the end and leave off where it was.
         //Convert the above date to 7/8/2017, 10:14 PM format

    }
    printf( "%s/%s/%s, %s:%s", date[1],date[0],date[2], date[4],date[5] );

    return 0;
}

标签: ctoken

解决方案


您只比较一个元素check

必须遍历.check

我在末尾添加了一个“哨兵”值NULLcheck标记表格的结尾。

风格:不要使用“侧边栏”评论。他们不必要地加宽了线条,使其难以容纳 80 个字符 [这是推荐的]。将这样的评论放在上面一行。

这是重构的代码:

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

// This code will take user input of date and use 2-d array to store strings
// using delimeter as well. It will list all
// the date in descending order seperating:
//   "Month", "Day", "Year", "Hour", "Minute", "Second"
// from:
//   "Saturday, July 8, 2017, 22:14:10"

int
main(void)
{
    char *check[] = {
        "Saturday", "Monday", "Tuesday", "Wednesday", "Thursday",
        "Sunday", "Friday",
#if 1
        NULL
#endif
    };
    char **day;

    // creates 2 day array
    char date[8][20];

    // creates order of time
    char *order[] = { "Month", "Day", "Year", "Hour", "Minute", "Second" };

    printf("Enter date separated by commas in following format:\n");
    printf(" Example: Saturday, July 8, 2017, 22:14:10\n");

    // array to store string initially
    char text[64];

    // reads strings
    fgets(text, 64, stdin);

    // this is the delimeter that will be helpful for seperating it to
    // different tokens
    char *delims = " ,:";
    char *token = strtok(text, delims);
    char **label = order;
    int r = 0;

    while (token) {
        // look for match on day of the week
        for (day = check;  *day != NULL;  ++day) {
            if (strcmp(token,*day) == 0)
                break;
        }

        // if _not_ day of the week, store the value
        if (*day == NULL) {
            strcpy(date[r], token);

            printf("%-8s: %s\n ", *label, date[r]);

            label++;
            r++;
        }

        // this is so that it will find null values at the end and leave off
        // where it was.
        token = strtok(NULL, delims);

        // Convert the above date to 7/8/2017, 10:14 PM format
    }

    printf("%s/%s/%s, %s:%s", date[1], date[0], date[2], date[4], date[5]);

    return 0;
}

推荐阅读