Closed. This question needs to be more focused。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。
                        
                        2年前关闭。
                                                                                            
                
        
que是我期望用户输入3个字符串,但是如果用户仅输入2个字符串,我的代码将停止,因为它期望另外1个字符串。但是我需要我的代码仅以2个字符串继续。所以我应该怎么做。

`enter code here`

#include <stdio.h>
#include <ctype.h>

int main()
{
    char a[100];
    char b[100];
    char c[100];
    printf("enter your name\n");
    scanf("%s %s %s",a,b,c);
    printf("%c%c%c",toupper(a[0]),toupper(b[0]),toupper(c[0]));
    return 0;
}

最佳答案

根据John Coleman的建议:

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

#define BUFFER_SIZE 300

int main()
{
    char strings[3][100] = {{},
                            {},
                            {}};
    char cInputBuffer[BUFFER_SIZE];
    const char delimeter[2] = " ";
    char* token;
    int i = 0;

    printf("Enter your name\n");
    scanf("%[^\n]", cInputBuffer);
    token = strtok(cInputBuffer, delimeter);

    while( token != 0 && i < 3)
    {
        sprintf(strings[i],"%.100s",token);
        token = strtok(NULL, delimeter);
        i++;
    }
    if(i > 1)
    {
        printf("%c%c%c",toupper(strings[0][0]),toupper(strings[1][0]),toupper(strings[2][0]));
    }
    else
    {
        printf("Enter at least personal and family name.");
    }
    return 0;
}

关于c - 用户的预期输入是3个字符串,但是如果用户只给出2个字符串,那么我希望我的代码继续进行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42527608/

10-09 21:03