c - C语言中如何从字符串中获取单个单词?

标签 c string

为了完成我正在编写的程序,我必须能够将字符串的各个部分放入堆栈中以供以后使用。例如,假设我有这个字符串:

“22 15 - 2 +”

理想情况下,我首先想从字符串中提取 22,将其放入一个单独的临时字符串中,然后按照我的意愿对其进行操作。这是我正在使用的代码,我认为它可以工作,但它非常复杂。

void evaluatePostfix(char *exp){
    stack *s = initStack();
    char *temp_str;
    char temp;
    int temp_len, val, a, b, i=0, j;
    int len = strlen(exp);

    while(len > 0){                      
        temp_str = malloc(sizeof(char)); //holds the string i am extracting
        j=0;                             //first index in temp_str
        temp = exp[i];                   //current value in exp, incremented later on the function
        temp_len = 1;                    //for reallocation purposes
        while(!isspace(temp)){           //if a white space is hit, the full value is already scanned
            if(ispunct(temp))            //punctuation will always be by itself
                break;                   //break if it is encountered
            temp_str = (char*)realloc(temp_str, temp_len+1); //or else reallocate the string to hold the new character
            temp_str[j] = temp;          //copy the character to the string
            temp_len++;                  //increment for the length of temp_str
            i++;                         //advance one value in exp
            j++;                         //advance one value in temp_str
            len--;                       //the number of characters left to scan is one less
            temp = exp[i];               //prepare for the next loop
        }                                //and so on, and so on...
    }                                    //more actions follow this, but are excluded
}                                       

就像我说的,过于复杂。有没有更简单的方法来提取此代码?我可以可靠地依赖于我需要提取的值和字符之间存在空格。

最佳答案

如果你善于使用库函数,那么strtok就是为了这个

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

int main()
{
   char str[80] = "22 15 - 2 +";
   const char s[2] = " ";
   char *token;

   /* get the first token */
   token = strtok(str, s);

   /* walk through other tokens */
   while( token != NULL ) 
   {
      printf( " %s\n", token );

      token = strtok(NULL, s);
   }

   return(0);
}

Reference

<小时/>

strtok(char *str, const char *delim) 的限制的一个问题是它不能同时处理多个字符串,因为它维护一个静态指针来存储索引直到它被解析(因此如果一次只处理一个字符串就足够了)。更好、更安全的方法是使用 strtok_r(char *str, const char *delim, char **saveptr)它显式地采用第三个指针来保存解析后的索引。

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

int main()
{
   char str[80] = "22 15 - 2 +";
   const char s[2] = " ";
   char *token, *saveptr;

   /* get the first token */
   token = strtok_r(str, s, &saveptr);

   /* walk through other tokens */
   while( token != NULL ) 
   {
      printf( " %s\n", token );

      token = strtok_r(NULL, s, &saveptr);
   }

   return(0);
}

关于c - C语言中如何从字符串中获取单个单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36240397/

相关文章:

c - 我无法理解这个结果

c - 在 MATLAB 和 C 中四舍五入到所需的数字

c - 基于\0 终止的循环无法正常工作?

java - 如何更改字符串数组中的字符串? java

javascript - 在Javascript中,如何将字符串转换为属性名称?

c++ - C++ 字符串类可以进行指针运算吗?

无法将字符串分配给数组

c - vim/c - 如何按字母顺序排列函数?

c - 在 C 中有效地提取 double 的小数部分

python - 如何用另一个数字替换字符串中的数字?