简体   繁体   English

如何从字符串中分离整数并在C中操作它们?

[英]How do I separate integers from a string and manipulate them in C?

I am scanning for a date in the format mm-dd-yyyy and I need to take out three ints and use them for multiple things. 我正在扫描格式为mm-dd-yyyy的日期,我需要取出三个整数并将它们用于多种用途。 How can I create three integers with mm dd and yyyy? 如何用dd和yyyy创建三个整数? Thanks! 谢谢!

Use the sscanf call from <stdio.h> . 使用来自<stdio.h>sscanf调用。

char line[80];
int m, d, y;

fgets(fgets(line, sizeof line, stdin);

sscanf(line, "%d-%d-%d", &m, &d, &y);

It is better to use fgets+sscanf instead of scanf directly, because scanf has the notorious \\n issue and does not check buffer lengths. 最好直接使用fgets + sscanf而不是scanf,因为scanf有一个臭名昭著的\\n问题,并且不检查缓冲区长度。

Try this to use fgets for all input. 尝试使用fgets进行所有输入。
The do/while loops will continue until valid input is taken. do / while循环将继续进行,直到获得有效输入为止。
This only validates the month. 这仅验证月份。 Something should be added to verify day and year. 应该添加一些内容来验证日期和年份。
Check the return of sscanf to make sure the needed items have been successfully scanned. 检查sscanf的返回,以确保已成功扫描所需的项目。

#include <stdio.h>

int main(){
    char line[100] = "";
    int scanned = 0;
    int valid = 0;
    int mm = 0;
    int dd = 0;
    int yyyy = 0;

    int x;

    do {
        printf("\n\t\t\t1) Input date (mm/dd/yyyy) to find days passed");
        printf("\n\t\t\t2) Input passed days to find date in the year");
        printf("\n\n\t\t\tYour choice (1/2): ");
        if ( !( fgets ( line, sizeof line, stdin))) {
            fprintf ( stderr, "problem getting input found EOF\n");
            return 1;
        }
        if ( ( scanned = sscanf(line, "%d", &x)) != 1) {
            valid = 0;
        }
        else {
            if ( x == 1 || x == 2) {
                valid = 1;
            }
            else {
                valid = 0;
            }
        }
    } while ( !valid);

    if(x == 1) {
        do {
            printf ( "enter date mm-dd-yyyy\n");
            if ( !( fgets ( line, sizeof line, stdin))) {
                fprintf ( stderr, "problem getting input found EOF\n");
                return 1;
            }
            if ( ( scanned = sscanf ( line, "%d-%d-%d", &mm, &dd, &yyyy)) != 3) {
                valid = 0;
            }
            else {
                valid = 1;
                //validate the values for mm dd and yyyy
                if ( mm < 1 || mm > 12) {
                    printf ( "invalid month try again\n");
                    valid = 0;
                }
            }
        } while ( !valid);
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从c中的字符串中分离出整数和运算符? - how to separate integers and operators from a string in c? 如何从文本文件中读取整数和字符并将它们存储在单独的数组中? - How do I read integers and characters from a text file and store them in separate arrays? 如何在C中将整数拆分为单独的多位整数? - How do I split an integer into separate multi digit integers in C? 如何在 C 编程中使用 Strtol 将包含逗号的整数与文本文件分开? - How do I separate integers that contain commas from a text file using Strtol in C programming? 如何将数字与双精度分开并将其作为int存储在C中? - How do I separate digits from a double and store them as an int in C? c - 如何在C中将字符串的一部分转换为不同的整数? - How do I make parts of a string into different integers in C? 在 C 中,如何从字符串中取出两个字符,并将它们分配为自己的字符串? - In C, how do I take two characters from a string, and assign them as a string of their own? 如何在C中为RSA算法处理大整数? - How to manipulate big integers for the RSA algorithm in C? 如何从结构数组中获取所有元素,并将它们添加到 c 中的字符串中? - How do I get all elements from a struct array, and add them into a string in c? 如何从C中的字符串解析整数序列? - How to parse sequence of integers from string in C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM