如果输入包含'b',则有多种方法可以在第二个字符之后拆分输入字符串,否则在第一个字符之后拆分。由于您正在处理1 或2,因此您需要做的就是确定'b' 是否存在。最简单的方法是使用strchr(),它将在给定的字符串中搜索第一次出现的字符,如果找到则返回指向该字符的指针,否则返回NULL。见man 3 strchr
所以您可以使用strchr 来测试'b' 是否存在,如果返回不是NULL,则在第二个字符之后拆分字符串,如果是NULL,则在第一个字符之后拆分。
使用三元组来设置输入读入缓冲区buf的拆分后大小的简单实现是:
char part2[MAXC]; /* buffer to hold 2nd part */
size_t split; /* number of chars to split */
/* if buf contains 'b', set split at 2, otherwise set at 1 */
split = strchr(buf, 'b') ? 2 : 1;
strcpy (part2, buf + split); /* copy part2 from buf */
buf[split] = 0; /* nul-terminate buf at split */
一种快速实现,允许您输入任意数量的字符串,它会在第一个或第二个字符之后拆分,具体取决于缺席或'b' 的存在:
#include <stdio.h>
#include <string.h>
#define MAXC 1024 /* if you need a constant, #define one (or more) */
int main (void) {
char buf[MAXC]; /* buffer to hold line of input */
fputs ("Enter a string to split (or [Enter] alone to exit)\n\n"
"string: ", stdout);
while (fgets (buf, MAXC, stdin)) { /* loop reading each line */
char part2[MAXC]; /* buffer to hold 2nd part */
size_t split; /* number of chars to split */
if (*buf == '\n') /* if [Enter] alone, exit */
break;
/* if buf contains 'b', set split at 2, otherwise set at 1 */
split = strchr(buf, 'b') ? 2 : 1;
strcpy (part2, buf + split); /* copy part2 from buf */
buf[split] = 0; /* nul-terminate buf at split */
printf (" part1: %s\n part2: %s\nstring: ", buf, part2);
}
}
(注意:如果你不熟悉三元操作符,简单的(test) ? if_true : if_false。上面只是if (strchar (buf, 'b') != NULL) split = 2; else split = 1;的简写)
使用/输出示例
$ ./bin/splitb
Enter a string to split (or [Enter] alone to exit)
string: look out
part1: l
part2: ook out
string: look out below
part1: lo
part2: ok out below
string:
如果这是您的意图,请告诉我。如果没有,我很乐意提供进一步的帮助。另外,如果您有任何问题,请告诉我。
根据评论编辑
目前还不清楚你的头文件中的笔记列表是什么,但你可以简单地使用一个字符串常量来包含笔记的字母,例如
#define NOTES "abcdefg" /* (that can be a string constant as well) */
(如果需要,您可以添加大写字母,或者您可以将输入转换为小写 - 任何适合您的)
如果您只需要在NOTES 字符串中查找其中一个字母的第一次出现,那么strpbrk() 将允许您返回指向NOTES 的第一个字符的指针在您的@ 中找到987654343@。 (您必须有某种方式来处理用户输入,例如"the note cflat",它将在第一个'e' 而不是'c' 上拆分,但您需要在此处提供更多细节)
另一个考虑因素是note 可以有多长。如果它始终是 1 个字符,那么您可以通过使用 strchr (NOTES, buf[0]) 与字符串中的第一个字符进行比较来简化(这改变了您通常考虑使用 strchr() 的方式 - 使用第一个字符串 NOTES 和从用户输入读取的第一个字符。
采用将"---cflat---" 分解为"---c" 和"flat---" 的通用方法,您的函数可能类似于:
int split_note_and_chord (char *string, char *note, char *chord)
{
char *p = strpbrk (string, NOTES); /* pointer to first of NOTES in string */
if (p != NULL) { /* if found */
strcpy (note, string); /* copy string to note */
note[p - string + 1] = 0; /* nul-terminate after note */
strcpy (chord, p + 1); /* copy rest to chord */
return 1; /* return success */
}
*note = 0; /* make note and chord empty-string */
*chord = 0;
return 0; /* return failure */
}
(注意:如果在NOTES 中没有找到字符,那么note 和chord 会被nul-terminating 变成空字符串返回零之前的第一个字符以指示未找到注释。)
类似于第一个的快速实现可能是:
#include <stdio.h>
#include <string.h>
#define MAXC 1024 /* if you need a constant, #define one (or more) */
#define NOTES "abcdefg" /* (that can be a string constant as well) */
int split_note_and_chord (char *string, char *note, char *chord)
{
...
}
int main (void) {
char buf[MAXC], /* buffer to hold line of input */
note[MAXC], /* buffer for node */
chord[MAXC]; /* buffer for chord */
fputs ("Enter a string with node and chord (or [Enter] alone to exit)\n\n"
"string: ", stdout);
/* loop reading each line until [Enter] alone */
while (fgets (buf, MAXC, stdin) && *buf != '\n') {
if (split_note_and_chord (buf, note, chord))
printf (" note : %s\n chord : %s\n", note, chord);
else
fputs ("\nerror: note not found in string.\n\n", stderr);
fputs ("string: ", stdout);
}
return 0;
}
(注意: 使用fgets() 将读取并包含用户按Enter 生成的'\n' buf,因此它也将被包含在内在复制到chord 的其余部分中。您可以使用buf[strcspn (buf, "\n")] = 0; 从buf 修剪它 - 或通过在使用strcspn() 作为nul 索引的调用中将chord 替换为buf 来从chord 修剪它- 终止于。)
(另请注意:您可以调整 MAXC 以满足您的需求——这就是为什么你首先声明一个常量——使其在文件顶部)
使用/输出示例
使用您的函数拆分各种输入会导致以下结果:
$ ./bin/splitb3
Enter a string with node and chord (or [Enter] alone to exit)
string: ---cflat---
note : ---c
chord : flat---
string: asharp
note : a
chord : sharp
string: bflat
note : b
chord : flat
string: hook
error: note not found in string.
string: c
note : c
chord :
有很多不同的方法可以做到这一点,以及如何最好地处理它取决于您如何在标题中定义音符和和弦 - 以及您对格式设置的限制(如果有的话)要求用户输入。如果您需要更多帮助,请编辑您的问题并添加标题的内容,以便我们知道它们是如何声明和定义的,并列出您希望对用户可以施加的任何约束输入。