我有一个包含一些字符串的字符数组 例如
char* arr = "ABCDEFGHIJKL ZMN OPQOSJFS"
和字符串
char* string = "ZMN"
有没有什么打印方法只在第一次出现string后打印arr的内容?
在这种情况下,它将打印 "OPQOSJFS"
sprintf
怎么样?如果是,我该怎么做?
最佳答案
sprintf
不会在这里帮助你;使用 strstr
查找出现的位置,然后从那里打印源字符串:
// note: your string constants should be of type 'const char*'
const char* haystack = "ABCDEFGHIJKL ZMN OPQOSJFS";
const char* needle = "ZMN";
// find occurence of the string
const char* out = strstr(haystack, needle);
if(out != NULL) {
// print string, starting from the end of the occurence
out += strlen(needle);
printf("The string is: %s", out);
}
关于c - 使用 sprintf 格式化 char 数组的打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31180052/