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

标签 c string loops

我还是 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}

当 DEF 在第二次调用 Replace_env 函数期间发送时,事情变得更加奇怪。 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 操作系统上运行这个。抱歉我的英语不好。

最佳答案

我知道你没有要求这个,但请考虑一下。学习 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;
}

关于c - 如何在C中正确操作字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19851549/

相关文章:

c - 代码结果说明

c++ - 计算一个字符串在一个字符串中出现的次数

c - 在 C 中,如何确保初始化字符串的长度小于指定的长度

c - 我们如何在 C 中通过指针获取整个字符串?

javascript - 与 Ajax 循环中的 onClick 不起作用

java - 需要帮助保存和继续用户输入

c - 在 C 中列出数组

python - 在 Python 中按地址拆分字符串,就像在 C 中一样(Python 的字符串切片)

javascript - 使用数字序列循环遍历 json 的不同属性

c - 在C中传递Void类型参数