我在进行比较时尝试在另一个字符串的末尾添加一个字符串。我将如何在Java中执行此操作(可能不是合法的Java代码-已有一段时间):
String input = "addl $1,%eax";
String[] registers = {"eax", "abx", "ebx", "edx"};
String s = "addl $1,%";
for (int i = 0 ; i < 4; i++) {
if (input.equals(s + registers[i])) {
printf("Match");
// write out optimized code with specified register
}
}
我完全不确定如何在C语言中执行此操作。我尝试了以下操作,但是我的程序不断崩溃(我认为是由于一些无用的指针):
...
char *in = "Hell";
char *pattern = "Hello";
const char *a[2];
a[0] = "e";
a[1] = "o";
char *result = strcat(in,a[1]);
if (strcmp(in, result) == 0) {
printf("Helloooooooooooooooo");
}
谁能启发我如何在C语言中进行这种字符串操作?
您可以尝试以下方法:
const char *input = "addl $1,%eax"; // For testing the code
char *registers[] = {"eax", "abx", "ebx", "edx"};
const char *s = "addl $1,%";
char temp[30]; // temp array to store possible commands in complete form
for (int i = 0 ; i < 4; i++) {
strcpy(temp, s); // Copy the common part to `temp`
if (strcmp(input, strcat(temp, registers[i])) == 0)
// Compare the input string with possible command string
{
printf("Match");
// write out optimized code with specified register
}
}
你可以阅读更多有关strcpy
,strcmp
而strcat
在这里
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句