仅打印c中第一个出现的子字符串

仅打印c中第一个出现的子字符串,c,substring,C,Substring,我是C语言的新手,我有这样的代码 #include <stdio.h> #include <string.h> int i = 0; int main() { char text[] = "..... $it is beautiful : $yes you are correct...."; char * sub = "$"; char * ret = strstr(text, sub); if (ret != NULL) {

我是C语言的新手,我有这样的代码

#include <stdio.h>
#include <string.h>

int i = 0;
int main()
{
    char text[] = "..... $it is beautiful : $yes you are correct....";
    char * sub = "$";
    char * ret = strstr(text, sub);

    if (ret != NULL)
    {
         printf("the statement is : %s", ret);
    }
}
#包括
#包括
int i=0;
int main()
{

char text[]=“…$它很漂亮:$是的,你是正确的…”; char*sub=“$”; char*ret=strstrstr(文本,sub); 如果(ret!=NULL) { printf(“语句为:%s”,ret); } }
我只想打印这部分代码:
$它很漂亮
。 有没有办法只打印那份声明?让我们假设这是全文的一部分,那么除了使用
strlen
之外还有什么想法吗? 当第二个
$
出现时,不应进行打印。这是我使用
的基本要求:
$
根据您的输出要求

#include <stdio.h>
#include <string.h>

int main() {
    char text[] = "..... $it is beautiful : $yes you are correct....";
    char * ret = strstr(text, "$");
    if (ret != NULL) {
        char *colon;
        if (colon = strrchr(ret, ':'))
            *colon = '\0';
        printf("The statement is: %s", ret);
    }
    return 0;
}
#包括
#包括
int main(){

char text[]=“…$它很漂亮:$是的,你是正确的…”; char*ret=strstrstr(文本,“$”); 如果(ret!=NULL){ 字符*冒号; if(冒号=strrchr(ret,,:')) *冒号='\0'; printf(“语句为:%s”,ret); } 返回0; }
输出

声明是:$它是美丽的

这应该行得通

 #include <stdio.h>
 #include <string.h>

 int i=0;
 int main()
{
    char text[] = "..... $it is beautiful : $yes you are correct....";
    char* sub = "$";
    char* ret = strstr(text,sub);
    char *out = malloc(strlen(text)+1); //To make the solution generic

    if (ret!=NULL )
     {
         sscanf(ret, "$%[^$^:]", out);
         printf("the statement is : [%s] \n",out );
     }

     free(out);
}
#包括
#包括
int i=0;
int main()
{

char text[]=“…$它很漂亮:$是的,你是正确的…”; char*sub=“$”; char*ret=strstrstr(文本,sub); char*out=malloc(strlen(text)+1);//使解决方案通用 如果(ret!=NULL) { sscanf(ret,$%[^$^:],out); printf(“语句是:[%s]\n”,out); } 免费(外出); }
你现在得到的是什么印刷品?$很漂亮:$是的,你是对的。。。。这是印刷品output@kaya很高兴这有帮助。