Arrays 在数组中的两个字符之间存储文本

Arrays 在数组中的两个字符之间存储文本,arrays,c,string,char,Arrays,C,String,Char,我有一个char数组char-txt[80]=“2017年11月1日$之前的一些文本”并需要将两个$之间的内容复制到一个字符串中,该字符串将为11/01/2017。如何使用函数执行此操作?假设您确定字符串中有2个$。。。您可以执行以下操作: char *first_dollar = strchr(txt, '$'); //get position of first dollar from the start of string char *second_dollar = strchr(first

我有一个char数组
char-txt[80]=“2017年11月1日$之前的一些文本”
并需要将两个
$
之间的内容复制到一个字符串中,该字符串将为
11/01/2017
。如何使用
函数执行此操作?

假设您确定字符串中有2个
$
。。。您可以执行以下操作:

char *first_dollar = strchr(txt, '$'); //get position of first dollar from the start of string
char *second_dollar = strchr(first_dollar + 1, '$'); //get position of first dollar starting
                                                    // from one position after the first dollar
char tocopy[20];
*second_dollar = '\0'; //change the value of last dollar to '\0'
strcpy(tocopy, first_dollar + 1); //copy into the place you want
*second_dollar = '$'; // put back the second dollar
如果您不确定字符串中是否包含2
$
,则应检查
strchr
的返回值,该值将为
NULL

是否必须使用字符串?有一种聪明的方法可以使用
sscanf

char txt[80] = "Some text before $11/01/2017$";
char t[20];
sscanf(txt, "%*[^$]$%[^$]", t);
printf("ORIGINAL TEXT: %s\nEXTRACTED TEXT: %s\n", txt, t);
scanf中的格式表示以下内容:

char *first_dollar = strchr(txt, '$'); //get position of first dollar from the start of string
char *second_dollar = strchr(first_dollar + 1, '$'); //get position of first dollar starting
                                                    // from one position after the first dollar
char tocopy[20];
*second_dollar = '\0'; //change the value of last dollar to '\0'
strcpy(tocopy, first_dollar + 1); //copy into the place you want
*second_dollar = '$'; // put back the second dollar
  • 忽略所有不是
    $
    的字符
  • 忽略1
    $
  • 读取所有字符,直到找到下一个
    $
    并将其存储在
    t

  • 假设您确定字符串中有2
    $
    。。。您可以执行以下操作:

    char *first_dollar = strchr(txt, '$'); //get position of first dollar from the start of string
    char *second_dollar = strchr(first_dollar + 1, '$'); //get position of first dollar starting
                                                        // from one position after the first dollar
    char tocopy[20];
    *second_dollar = '\0'; //change the value of last dollar to '\0'
    strcpy(tocopy, first_dollar + 1); //copy into the place you want
    *second_dollar = '$'; // put back the second dollar
    
    如果您不确定字符串中是否包含2
    $
    ,则应检查
    strchr
    的返回值,该值将为
    NULL

    是否必须使用字符串?有一种聪明的方法可以使用
    sscanf

    char txt[80] = "Some text before $11/01/2017$";
    char t[20];
    sscanf(txt, "%*[^$]$%[^$]", t);
    printf("ORIGINAL TEXT: %s\nEXTRACTED TEXT: %s\n", txt, t);
    
    scanf中的格式表示以下内容:

    char *first_dollar = strchr(txt, '$'); //get position of first dollar from the start of string
    char *second_dollar = strchr(first_dollar + 1, '$'); //get position of first dollar starting
                                                        // from one position after the first dollar
    char tocopy[20];
    *second_dollar = '\0'; //change the value of last dollar to '\0'
    strcpy(tocopy, first_dollar + 1); //copy into the place you want
    *second_dollar = '$'; // put back the second dollar
    
  • 忽略所有不是
    $
    的字符
  • 忽略1
    $
  • 读取所有字符,直到找到下一个
    $
    并将其存储在
    t

  • 我不知道你为什么要用string.h

    供您参考,这是不带string.h方法的

    更新

    #include <stdio.h>
    #include <string.h>
    int main(){
      char txt[80] = "Some text before $21/01/2017$ and $32/01/2017$ and $$ end $abc$";
      char get[80] = { '\0' };
      int i = 0, k = -1, j = 0;
      int len = strlen( txt ); // Get length
      for ( i = 0 ; i < len ; i++ ){
        bool   find = false;
        for ( i  ; txt[i] != '$' && txt[i] != '\0' ; i++ ); // Find '$' location
          if ( txt[i] == txt[i+1] && txt[i] == '$' ) { // Check $$ case
            find = true;
            get[++k] = ' ';
          } // if
          for ( j = i + 1 ; txt[j] != '$' && txt[j] != '\0' ; j++ ){
            find = true;
            get[++k] =  txt[j];
          } // for
    
       if ( find == true ) get[++k] = ' '; // add space
        i = j ;
      } // for
    
      get[k] = '\0'; // remove last space
      printf( "%s", get );
      return 0;
    } // main()
    

    我不知道你为什么要用string.h

    供您参考,这是不带string.h方法的

    更新

    #include <stdio.h>
    #include <string.h>
    int main(){
      char txt[80] = "Some text before $21/01/2017$ and $32/01/2017$ and $$ end $abc$";
      char get[80] = { '\0' };
      int i = 0, k = -1, j = 0;
      int len = strlen( txt ); // Get length
      for ( i = 0 ; i < len ; i++ ){
        bool   find = false;
        for ( i  ; txt[i] != '$' && txt[i] != '\0' ; i++ ); // Find '$' location
          if ( txt[i] == txt[i+1] && txt[i] == '$' ) { // Check $$ case
            find = true;
            get[++k] = ' ';
          } // if
          for ( j = i + 1 ; txt[j] != '$' && txt[j] != '\0' ; j++ ){
            find = true;
            get[++k] =  txt[j];
          } // for
    
       if ( find == true ) get[++k] = ' '; // add space
        i = j ;
      } // for
    
      get[k] = '\0'; // remove last space
      printf( "%s", get );
      return 0;
    } // main()
    

    $

    这可以通过简单的
    循环、读取和复制字符来完成。
    在下面的代码中,参数
    inside
    指示我们当前是否在两个
    $

    如果有效地找到了两个$,则函数返回1

    #include <stdio.h>
    #include <string.h>
    
    // return 1 if two $ have been found, 0 elsewhere
    int extract (char *in, char *out, char c) {
        if (in == NULL) return 0;
        int size = strlen(in);
        int inside = 0;
        int n = 0;      // size new string
        for (int i = 0; i < size; ++i) {
            if(in[i] == c) {
                if (inside) {
                    inside = 2;
                    break;  // 2nd $
                }
                inside = 1;         // 1st $
            } else {
                if (inside) {       // copy
                    out[n++] = in[i];
                }
            }
        }
        out[n++] = '\0';
        return inside == 2;
    }
    
    int main() {
        char txt[80] = "Some text before $11/01/2017$";
        char txt_extracted[80];
        int check = extract (txt, txt_extracted, '$');
        if (check) printf ("%s\n", txt_extracted);
        else printf ("two $ were not found\n");
        return 0;
    }
    
    #包括
    #包括
    //如果找到两个$1,则返回1,其他地方返回0
    整数提取(字符*输入,字符*输出,字符c){
    if(in==NULL)返回0;
    int size=strlen(英寸);
    内部整数=0;
    int n=0;//调整新字符串的大小
    对于(int i=0;i
    $之间提取文本

    这可以通过简单的
    循环、读取和复制字符来完成。
    在下面的代码中,参数
    inside
    指示我们当前是否在两个
    $

    如果有效地找到了两个$,则函数返回1

    #include <stdio.h>
    #include <string.h>
    
    // return 1 if two $ have been found, 0 elsewhere
    int extract (char *in, char *out, char c) {
        if (in == NULL) return 0;
        int size = strlen(in);
        int inside = 0;
        int n = 0;      // size new string
        for (int i = 0; i < size; ++i) {
            if(in[i] == c) {
                if (inside) {
                    inside = 2;
                    break;  // 2nd $
                }
                inside = 1;         // 1st $
            } else {
                if (inside) {       // copy
                    out[n++] = in[i];
                }
            }
        }
        out[n++] = '\0';
        return inside == 2;
    }
    
    int main() {
        char txt[80] = "Some text before $11/01/2017$";
        char txt_extracted[80];
        int check = extract (txt, txt_extracted, '$');
        if (check) printf ("%s\n", txt_extracted);
        else printf ("two $ were not found\n");
        return 0;
    }
    
    #包括
    #包括
    //如果找到两个$1,则返回1,其他地方返回0
    整数提取(字符*输入,字符*输出,字符c){
    if(in==NULL)返回0;
    int size=strlen(英寸);
    内部整数=0;
    int n=0;//调整新字符串的大小
    对于(int i=0;i
    有一个名为strtok的函数。() 这是一段关于它的视频:

    我尝试了以下代码:

    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char txt[] = "Some text before $11/01/2017$, some text, $11/04/2018$ another text more and more text $01/02/2019$";
    int skip = 0;
    
    char* piece = strtok(txt, "$");
    
    while(piece != NULL)
    {
        piece = strtok(NULL, "$");
    
        if(piece == NULL)
            break;
    
        if(skip != 1)
        {
            skip = 1;    
            printf("%s \n", piece);
        }
        else
            skip = 0;
        
    }
    
        return 0;
    }
    

    有一个名为strtok的函数。() 这是一段关于它的视频:

    我尝试了以下代码:

    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char txt[] = "Some text before $11/01/2017$, some text, $11/04/2018$ another text more and more text $01/02/2019$";
    int skip = 0;
    
    char* piece = strtok(txt, "$");
    
    while(piece != NULL)
    {
        piece = strtok(NULL, "$");
    
        if(piece == NULL)
            break;
    
        if(skip != 1)
        {
            skip = 1;    
            printf("%s \n", piece);
        }
        else
            skip = 0;
        
    }
    
        return 0;
    }
    

    这里有一个函数。无手井角箱。使用
    string.h
    函数

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    char *texBetween(const char *str, char ch)
    {
        char *result = NULL;
        const char *start, *end;
    
        if(str)
        {
            start = strchr(str, ch);
            if(start)
            {
                end = strchr(start + 1, ch);
                if(end)
                {
                    result = malloc(end - start);
                    if(result)
                    {
                        memcpy(result, start + 1, end - start - 1);
                        result[end - start] = 0;
                    }
                }
            }
        }
        return result;
    }
    
    int main()
    {
        char *result;
        printf("\"%s\"\n", (result = texBetween("$$", '$')) ? result : "ERROR"); free(result);
        printf("\"%s\"\n", (result = texBetween("$ $", '$')) ? result : "ERROR"); free(result);
        printf("\"%s\"\n", (result = texBetween("$test$", '$')) ? result : "ERROR"); free(result);
        printf("\"%s\"\n", (result = texBetween("test$$", '$')) ? result : "ERROR"); free(result);
        printf("\"%s\"\n", (result = texBetween("test$test1$", '$')) ? result : "ERROR"); free(result);
        printf("\"%s\"\n", (result = texBetween("test$1234$test$dfd", '$')) ? result : "ERROR"); free(result);
        printf("\"%s\"\n", (result = texBetween(NULL, '$')) ? result : "ERROR"); free(result);
        printf("\"%s\"\n", (result = texBetween("", '$')) ? result : "ERROR"); free(result);
        printf("\"%s\"\n", (result = texBetween("$", '$')) ? result : "ERROR"); free(result);
    
    }
    
    #包括
    #包括
    #包括
    字符*texBetween(常量字符*str,字符ch)
    {
    char*result=NULL;
    常量字符*开始,*结束;
    如果(str)
    {
    start=STRCR(str,ch);
    如果(启动)
    {
    结束=strchr(开始+1,通道);
    若(完)
    {
    结果=malloc(结束-开始);
    如果(结果)
    {
    memcpy(结果,开始+1,结束-开始-1);
    结果[结束-开始]=0;
    }
    }
    }
    }
    返回结果;
    }
    int main()
    {
    字符*结果;
    printf(“\%s\”\n),(结果=texBetween(“$$”,“$”)?结果:“错误”);自由(结果);
    printf(“\%s\”\n),(结果=texBetween(“$$”,“$”)?结果:“错误”);自由(结果);
    printf(“\%s\”\n),(result=texBetween(“$test$”,“$”)?结果:“ERROR”);free(result);
    printf(“\%s\”\n),(结果=texBetween(“test$$”,“$”)?结果:“ERROR”);free(结果);
    printf(“\%s\”\n),(result=texBetween(“test$test1$”,“$”))?result:“ERROR”);free(result);
    printf(“\%s\”\n),(结果=texBetween(“test$1234$test$dfd”,“$”)?结果:“ERROR”);免费(结果);
    printf(“\%s\”\n),(result=texBetween(NULL,$)?结果:“ERROR”);free(result);
    printf(“\%s\”\n),(result=texBetween(“,“$”)?结果:“错误”);free(结果);
    printf(“\%s\”\n),(结果=texBetween(“$”,“$”)?结果:“错误”);自由(结果);
    }
    

    这里有一个函数。无手井角箱。使用
    string.h
    函数

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    char *texBetween(const char *str, char ch)
    {
        char *result = NULL;
        const char *start, *end;
    
        if(str)
        {
            start = strchr(str, ch);
            if(start)
            {
                end = strchr(start + 1, ch);
                if(end)
                {
                    result = malloc(end - start);
                    if(result)
                    {
                        memcpy(result, start + 1, end - start - 1);
                        result[end - start] = 0;
                    }
                }
            }
        }
        return result;
    }
    
    int main()
    {
        char *result;
        printf("\"%s\"\n", (result = texBetween("$$", '$')) ? result : "ERROR"); free(result);
        printf("\"%s\"\n", (result = texBetween("$ $", '$')) ? result : "ERROR"); free(result);
        printf("\"%s\"\n", (result = texBetween("$test$", '$')) ? result : "ERROR"); free(result);
        printf("\"%s\"\n", (result = texBetween("test$$", '$')) ? result : "ERROR"); free(result);
        printf("\"%s\"\n", (result = texBetween("test$test1$", '$')) ? result : "ERROR"); free(result);
        printf("\"%s\"\n", (result = texBetween("test$1234$test$dfd", '$')) ? result : "ERROR"); free(result);
        printf("\"%s\"\n", (result = texBetween(NULL, '$')) ? result : "ERROR"); free(result);
        printf("\"%s\"\n", (result = texBetween("", '$')) ? result : "ERROR"); free(result);
        printf("\"%s\"\n", (result = texBetween("$", '$')) ? result : "ERROR"); free(result);
    
    }
    
    #公司