Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 用多个字符替换C数组中的字符的标准函数?_String_Arrays - Fatal编程技术网

String 用多个字符替换C数组中的字符的标准函数?

String 用多个字符替换C数组中的字符的标准函数?,string,arrays,String,Arrays,我有一个字符数组,里面有一些特殊的字符。我必须用另一个预定义的多个字符替换数组中的这个特殊字符 假设特殊字符和相应的值如下所示: ", @--->&35 目前,我正在比较输入数组中每个字符的特殊字符,并将其替换为输出数组中的预定义值,如下面的代码段所示: char istr[100] = "This is < a input string @ 123"; char ostr[100]; char * p = istr; int ic = 0; int

我有一个字符数组,里面有一些特殊的字符。我必须用另一个预定义的多个字符替换数组中的这个特殊字符

假设特殊字符和相应的值如下所示:

<-->", @--->&35

目前,我正在比较输入数组中每个字符的特殊字符,并将其替换为输出数组中的预定义值,如下面的代码段所示:

 char istr[100] = "This is < a input string @ 123";
    char ostr[100];
    char * p = istr;
    int ic = 0;
    int oc = 0;
    while(*p)
    {
         if(*p == '<')
         {
             ic++;
             ostr[oc++] = '&';
             ostr[oc++] = '#';
             ostr[oc++] = '3';
             ostr[oc++] = '4';
         }
         if(*p == '@')
         {
             ic++;
             ostr[oc++] = '&';
             ostr[oc++] = '#';
             ostr[oc++] = '3';
             ostr[oc++] = '5';
         }

         else
         {
             ostr[oc++] = istr[ic++];
         }

        p++;
    }
    ostr[oc++] = '\0';
    printf("%s",ostr);
char istr[100]=“这是如果(*p==”,则需要调整数组大小。这样做不会将多个字符添加到字符串中,而是替换字符串中的现有值,或者使ostr比istr大4倍(如果您的大小写限制为最多用4个字符替换1个字符),或者使用malloc+relloc。