gpt4 book ai didi

c - 使用 sprintf 格式化 char 数组的打印

转载 作者:太空宇宙 更新时间:2023-11-04 00:24:23 25 4
gpt4 key购买 nike

我有一个包含一些字符串的字符数组例如

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com