我正在尝试编写一个if else语句,该语句查看用户输入,然后在字符串包含字母b的情况下在index [1]之后拆分,或者在字符串输入不包含字母b的情况下在index [0]之后拆分。我将如何处理?对C来说还很陌生,所以不太确定。
这是我到目前为止所拥有的...我认为我在正确的道路上,正在尝试弄清楚如何完成它,以便它完成我想要的工作。
int split_note_and_chord(char* string, char* note, char* chord)
{
for(user input doesnt have b in it)
{
if(i = 0; i <index; i++)
{
note[i] = string[i];
}
note[index] = 0;
else{ if(i = 0; i < index; i++)
{
note[i] = strlen(string[2]);
}
}
}
答案 0 :(得分:0)
C字符串不过是一个char数组
string.h
提供了方便的功能来检查字符串内容
您可以使用if条件以及strstr
和strchr
函数来实现逻辑
例如
#include <stdio.h>
#include <string.h>
int main () {
const char *input = "backwards";
char *ret;
ret = strstr(input, "b");
if( ret != NULL ) {
} else {
}
}
如果strstr
不存在,NULL
将返回b
如果要将第二个参数设为单个字符strchr
strchr(input, 'b');
。
答案 1 :(得分:0)
如果输入包含'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()
将允许您执行将指针返回到{{1 }}在您的NOTES
中找到。 (您必须有某种方式来处理用户输入的内容,例如string
,它将在第一个"the note cflat"
而不是'e'
上分割,但是您需要在此提供更多详细信息)>
另一个考虑因素是'c'
可以有多长。如果始终为1个字符,则可以通过使用note
与字符串中的第一个字符进行比较来简化操作(这将改变您通常考虑使用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 */
}
中找不到字符,则 nul-将NOTES
和note
设为空字符串。在第一个字符处终止,然后返回零以表示未找到注释。)
与第一个类似的快速实现可能是:
chord
(注意:使用#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()
,这是由于用户在{{1}中按 Enter 因此,它也将包含在复制到'\n'
的其余部分中。您可以使用buf
将其从chord
修剪掉,也可以用buf[strcspn (buf, "\n")] = 0;
来代替buf
中使用chord
作为n终止处的索引。)
(也请注意:,您可以调整chord
使其适合您的需要-这就是为什么您首先声明一个常数的原因,以使其简单地更改为一行在文件顶部)
使用/输出示例
使用您的函数来分割各种输入将导致以下结果:
buf
执行此操作的方法有很多很多,如何最好地实现它取决于您如何在标题中定义音符和和弦-以及对格式设置的限制(如果有的话)要求用户输入。如果您需要更多帮助,请编辑您的问题并 Add 标头内容,以便我们知道如何声明和定义标头,以及列出您希望对用户施加的任何约束输入。