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

标签 c printing

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

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/

相关文章:

c - 从函数返回矩阵

c - 为什么程序打印0.00

c - Win32 PrintDlg、PrintDlgEx、崩溃和怪异

C vfprintf 函数不适用于字符串参数

java - 如何在gwt中打印一些元素

printing - 是否有一种可移植的方法来打印来自 C 预处理器的消息?

c - 如何在流中部分发送文本文件?

c - 从 C 中的文本文件中读取\n 作为真正的 Feed Line 字符

java - BufferedOutputStream 未写入 standardIO

php - 如何在 php/javascript 中打印输出时隐藏可见的超链接