So i have been working on an C project and im stuck on this problem, basically Im making a program where the user can enter their name and I later print it out to a file, the first and last name of the user is split with a comma however, optional whitespace in around the comma is allowed. This is what i have been using the read the user names:
所以我一直在研究一个C项目并且我坚持这个问题,基本上我正在创建一个程序,用户可以输入他们的名字,然后我将它打印到一个文件,用户的名字和姓氏被分成一个但是,逗号周围的可选空格是允许的。这就是我一直在使用的读取用户名:
char lastName[20];
char firstName[20];
char line[LINESIZE];
while(fgets(line,LINESIZE,stdin)) {
if((sscanf(line," %s , %s ",lastName,firstName)) == 2) {
/*process name */
}
}
However, the only time it reads input successfully is when the user inputs:
但是,它成功读取输入的唯一时间是用户输入:
john , doe
which matches the %s , %s I have, how can i make it such that something like:
哪个匹配%s,%s我有,我怎么能这样做,像:
john, doe
john ,doe
Can all work?
一切都可以吗?
I have also tried
我也试过了
sscanf(line,"%s%[],%[]%s");
This doesnt cause compilation error, but it doesnt process input meaning it doesnt match the %s , %s
这不会导致编译错误,但它不会处理输入,这意味着它与%s,%s不匹配
2 个解决方案
#1
1
This will isolate the names so that any permutation of "Doe , John"
filters out the whitespace
这将隔离名称,以便“Doe,John”的任何排列过滤掉空白
#include <stdio.h>
#include <string.h>
#define LINESIZE 100
int main(void) {
char lastName[20] = {0};
char firstName[20] = {0};
char line[LINESIZE];
char *first = NULL, *last = NULL;
if (NULL == fgets(line,LINESIZE,stdin))
return 1;
last = strtok (line, ", \t\r\n");
if (last)
first = strtok (NULL, ", \t\r\n");
if (last && first) {
sprintf(firstName, "%.19s", first);
sprintf(lastName, "%.19s", last);
printf ("%s %s\n", firstName, lastName);
}
return 0;
}
#2
4
You can modify the sscanf
format to have it perform a more stringent test:
您可以修改sscanf格式以使其执行更严格的测试:
char eol[2];
if (sscanf(line, " %19[a-zA-Z-] , %19[a-zA-Z-]%1[\n]", lastName, firstName, eol) == 3) ...
sscanf
will verify that the user typed exactly 2 words made of letters separated by a comma and optional spaces and followed by a line feed.
sscanf将验证用户是否正好键入了由逗号和可选空格分隔的字母组成的2个单词,后跟换行符。
But I strongly encourage you to parse the input yourself instead of relying on sscanf
. It is not difficult, much more precise and flexible, and less error prone:
但我强烈建议您自己解析输入,而不是依赖于sscanf。它不难,更精确,更灵活,更不容易出错:
char lastName[LINESIZE];
char firstName[LINESIZE];
char line[LINESIZE];
while(fgets(line,LINESIZE,stdin)) {
char *p = line, *q;
while (isspace((unsigned char)*p)) p++;
for (q = lastName; isalpha((unsigned char)*p); p++) {
*q++ = *p;
}
*q = '\0';
while (isspace((unsigned char)*p)) p++;
if (*p == ',') p++;
while (isspace((unsigned char)*p)) p++;
for (q = firstName; isalpha((unsigned char)*p); p++) {
*q++ = *p;
}
*q = '\0';
while (isspace((unsigned char)*p)) p++;
if (*lastName && *firstName && !*p) {
// format is OK
printf("Hello %s %s\n", firstName, lastName);
}
}
#1
1
This will isolate the names so that any permutation of "Doe , John"
filters out the whitespace
这将隔离名称,以便“Doe,John”的任何排列过滤掉空白
#include <stdio.h>
#include <string.h>
#define LINESIZE 100
int main(void) {
char lastName[20] = {0};
char firstName[20] = {0};
char line[LINESIZE];
char *first = NULL, *last = NULL;
if (NULL == fgets(line,LINESIZE,stdin))
return 1;
last = strtok (line, ", \t\r\n");
if (last)
first = strtok (NULL, ", \t\r\n");
if (last && first) {
sprintf(firstName, "%.19s", first);
sprintf(lastName, "%.19s", last);
printf ("%s %s\n", firstName, lastName);
}
return 0;
}
#2
4
You can modify the sscanf
format to have it perform a more stringent test:
您可以修改sscanf格式以使其执行更严格的测试:
char eol[2];
if (sscanf(line, " %19[a-zA-Z-] , %19[a-zA-Z-]%1[\n]", lastName, firstName, eol) == 3) ...
sscanf
will verify that the user typed exactly 2 words made of letters separated by a comma and optional spaces and followed by a line feed.
sscanf将验证用户是否正好键入了由逗号和可选空格分隔的字母组成的2个单词,后跟换行符。
But I strongly encourage you to parse the input yourself instead of relying on sscanf
. It is not difficult, much more precise and flexible, and less error prone:
但我强烈建议您自己解析输入,而不是依赖于sscanf。它不难,更精确,更灵活,更不容易出错:
char lastName[LINESIZE];
char firstName[LINESIZE];
char line[LINESIZE];
while(fgets(line,LINESIZE,stdin)) {
char *p = line, *q;
while (isspace((unsigned char)*p)) p++;
for (q = lastName; isalpha((unsigned char)*p); p++) {
*q++ = *p;
}
*q = '\0';
while (isspace((unsigned char)*p)) p++;
if (*p == ',') p++;
while (isspace((unsigned char)*p)) p++;
for (q = firstName; isalpha((unsigned char)*p); p++) {
*q++ = *p;
}
*q = '\0';
while (isspace((unsigned char)*p)) p++;
if (*lastName && *firstName && !*p) {
// format is OK
printf("Hello %s %s\n", firstName, lastName);
}
}