繁体   English   中英

如何在C中正确操作字符串

[英]How to manipulate string properly in C

我还是C语言新手。我正在执行环境变量任务,并且在处理字符串时遇到问题。 我想传递一个表示环境变量的变量,如果该字符串与环境键相同,则用环境值替换一个具有$ {...}的值。 以下是代码:

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

void replace_env(char *string, char *env)
{
    int y = 0;
    int x = 0;
    int j = 0;
    int i = 0;
    int n = 0;
    int val_length;
    int location2[100];
    char *tmp3[BUFSIZ];
    char env_key[BUFSIZ];
    char env_val[BUFSIZ];
    char env_var[sizeof(env)][BUFSIZ];
    char user_input[BUFSIZ];
    char final_string[BUFSIZ];
    char tmp_key[100][BUFSIZ];

    tmp3[x]=env;
    strncpy(env_var[x],tmp3[x],sizeof(tmp3));

    for(x=0;env_var[y][x] != '=';x++)    //this is to get the environment key
    {
            env_key[x] = env_var[y][x];
    }
    x++;
    for(j=0;env_var[y][j] != '\0';j++)    //this is to get the environment value
    {
            env_val[j]=env_var[y][x];
            x++;
    }
    val_length = strlen(env_val);
    j=0;
    y=0;
    strncpy(user_input,string,sizeof(user_input));

    for(x = 0;user_input[x] !='\0';x++)
    {
     if (user_input[x] =='$')
      {
        x++;
        if(user_input[x] == '{')
         {
           x++;
           y=0;
           while(user_input[x]!='}')
           {
             tmp_key[i][y] = user_input[x];
             x++;
             y++;
           }
          i++;
          }
       }
    }
    tmp_key[i][y]='\0';
    i=0;
    for(x = 0;user_input[x] !='\0';x++)    //I think my problem is starting from here.
    {
     if (user_input[x] !='$')
      {
       final_string[j]=user_input[x];
       j++;
      }
      else
          {
           x++;
           if((user_input[x]== '{')&&(strncmp(tmp_key[i],env_key,sizeof(tmp_key))==0))
           {
            while(user_input[x]!='}')
            {
             x++;
            }
            strcat(final_string,env_val);
            j=j+val_length;
           }
           else    
              {
                final_string[j]=user_input[x];
                j++;
              }
         }
    }
    printf("output result = %s \n",final_string);
}

int main() {
    char s[100];
    sprintf(s, "jack${ABC}zack${DEF}");
    replace_env(s, "ABC=/home/fikrie/Documents");
    replace_env(s, "DEF=/tmp");
    if (strcmp(s, "jack/home/fikrie/Documentszack/tmp")==0) {
            printf("pass\n");
    } else {
            printf("fail\n");
    }
    printf("--------------------------------------------------------\n");

return 0;
}

为了更加清楚,结果如下:

env_var = ABC=/home/fikrie/Documents 
env_key = ABC 
env_val = /home/fikrie/Documents 
input = jack${ABC}zack${DEF} 
after strcat result is = jack/home/fikrie/Documents 
j value is 26 
after strcat result is = jack/home/fikrie/Documentszack/home/fikrie/Documents 
j value is 52 
output result = jack/home/fikrie/Documentszack/home/fikrie/Documents 
env_var = DEF=/tmp 
env_key = DEF 
env_val = /tmp 
input = jack${ABC}zack${DEF} 
output result = jack{ABC}zack{DEF}ocumentszack/home/fikrie/Documents 
fail
--------------------------------------------------------

如您所见,ABC发送到了replace_env函数中。 并且它确实正确替换了$ {ABC},后跟一个字符串zack。然后就会出现问题,其中$ {DEF}被ABC密钥替换而不是保持为$ {DEF}

在第二次调用replace_env函数期间发送DEF时,事情变得更加棘手。 ABC和DEF均无法识别。 更糟糕的是,后面的角色仍然在那里。

我的期望是:

For the first call of replace_env:
jack/home/Fikrie/Documentszack${DEF}

For the second call of replace_env:
jack/home/Fikrie/Documentszacl/tmp

after the strcmp passed, the final_string will be cleared again.

非常感谢所有帮助。 我不希望得到答案。 我更喜欢知识或指导,而不是一味地解决它。 只需要对我的错误进行清楚的解释,因为我已经编辑了近一个月的代码,而现在一切看起来都变得很模糊。 我知道有使用内存功能,分配等方法解决它的方法。但是此任务是关于字符串操作的。 我在Ubuntu OS上运行它。 对不起,我的英语不好。

我知道您没有要求,但是请考虑一下。 学习C字符串函数值得您花时间。

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

void sub(char *s, char *env, char *value) {
        char buf[BUFSIZ], *src = s, *dst = buf;
        int n = strlen(env);
        while(*src) {
                if(strncmp(src, env, n) == 0) {
                        dst += strlen(strcpy(dst, value));
                        src += strlen(env);
                } else {
                        *dst++ = *src++;
                }
        }
        *dst = 0;
        strcpy(s, buf);
}

void replace_env(char *s, char *env) {
        char copy[BUFSIZ], tmp[BUFSIZ];
        strcpy(copy, env);
        char *eq = strchr(copy, '=');
        if(eq == 0) {
                printf("No '=' found in '%s'\n", env);
                return;
        }
        *eq = 0;
        sprintf(tmp, "${%s}", copy);
        sub(s, tmp, eq+1);
}

int main() {
    char s[100];
    sprintf(s, "jack${ABC}zack${DEF}");
    replace_env(s, "ABC=/home/fikrie/Documents");
    replace_env(s, "DEF=/tmp");
    if (strcmp(s, "jack/home/fikrie/Documentszack/tmp")==0) {
            printf("pass\n");
    } else {
            printf("fail\n");
    }
    printf("--------------------------------------------------------\n");
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM