如何在C中使用strtok删除char数组中的最后一个字符串?

发布于 2024-12-13 15:49:39 字数 698 浏览 2 评论 0原文

我有一个函数接受一些值作为 char array[] 参数。

这些值用分号 (';') 分隔。

例如: "hello;dear;John"

所以我试图找到一种方法,使用 strtok 删除最后一个字符串,即 "John " 在最后一个分号之后。

int remove(char lastName[]){


}

*更具体地说,

我创建了这个函数,它删除由分号分隔的值:

int remove(char variable_Name[]){

        char *value_toRemove = getenv(variable_Name);
        char last_semicolon[] = ";";
        char *result = NULL;
        result = strtok( value_toRemove, last_semicolon );
        while( result != NULL ) {
        result = strtok( NULL, last_semicolon );
        }
        return NULL;    
}

但是该函数在找到分号后会删除所有内容。

I have a function which accepts some values as char array[] parameters.

These values are separated with semicolons (';').

For example: "hello;dear;John"

So I'm trying to figure out a way by using strtok to delete the last string, which is "John" after the last semicolon.

int remove(char lastName[]){


}

*To be more specific

I have created this function which removes values separated by semicolons:

int remove(char variable_Name[]){

        char *value_toRemove = getenv(variable_Name);
        char last_semicolon[] = ";";
        char *result = NULL;
        result = strtok( value_toRemove, last_semicolon );
        while( result != NULL ) {
        result = strtok( NULL, last_semicolon );
        }
        return NULL;    
}

But the function deletes everything after it finds a semicolon.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

腻橙味 2024-12-20 15:49:39

strrchr 将查找该字符的最后一次出现。

因此,如果您不介意修改原始字符串,那么它应该像

int remove(char *lastName){
   char *pos = strrchr(lastName, ';');
   if (pos) {
      *pos = 0;
      return pos-lastName;
   }
   return 0;
}

手册页一样简单 此处< /a>

strrchr will find the last occurance of the character.

Sor if you don't mind modifyint the original string then it should be as simple as

int remove(char *lastName){
   char *pos = strrchr(lastName, ';');
   if (pos) {
      *pos = 0;
      return pos-lastName;
   }
   return 0;
}

Man Page here

廻憶裏菂餘溫 2024-12-20 15:49:39
char *last_semi = strrchr(lastName, ';');

if (last_semi != NULL)
   *last_semi = '\0';

编辑:作为对您的评论的回应,它确实有效。这就是我的做法,我已经包含了整个程序来显示输出示例:

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

char *remove_end(char *str, char c)
{
   char *last_pos = strrchr(str, c);

   if (last_pos != NULL) {
      *last_pos = '\0';
      return last_pos + 1; /* pointer to the removed part of the string */
   }

   return NULL;  /* c wasn't found in the string */
}

int main(void)
{
   char s1[] = "hello;dear;John";
   char s2[] = "nothing to remove";
   char *removed;

   /* s1 */
   printf("Original string: %s\n", s1);
   removed = remove_end(s1, ';');
   printf("New string: %s\n", s1);
   printf("Removed: %s\n", removed ? removed : "NOTHING");
   /* s2 */
   printf("Original string: %s\n", s2);
   removed = remove_end(s2, ';');
   printf("New string: %s\n", s2);
   printf("Removed: %s\n", removed ? removed : "NOTHING");

   return 0;
}

输出:

Original string: hello;dear;John
New string: hello;dear
Removed: John
Original string: nothing to remove
New string: nothing to remove
Removed: NOTHING

您也可以实时尝试 这里

char *last_semi = strrchr(lastName, ';');

if (last_semi != NULL)
   *last_semi = '\0';

EDIT: In response to your comment, it does work. This is how I'd do it and I've included the whole program to show an example of the output:

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

char *remove_end(char *str, char c)
{
   char *last_pos = strrchr(str, c);

   if (last_pos != NULL) {
      *last_pos = '\0';
      return last_pos + 1; /* pointer to the removed part of the string */
   }

   return NULL;  /* c wasn't found in the string */
}

int main(void)
{
   char s1[] = "hello;dear;John";
   char s2[] = "nothing to remove";
   char *removed;

   /* s1 */
   printf("Original string: %s\n", s1);
   removed = remove_end(s1, ';');
   printf("New string: %s\n", s1);
   printf("Removed: %s\n", removed ? removed : "NOTHING");
   /* s2 */
   printf("Original string: %s\n", s2);
   removed = remove_end(s2, ';');
   printf("New string: %s\n", s2);
   printf("Removed: %s\n", removed ? removed : "NOTHING");

   return 0;
}

Output:

Original string: hello;dear;John
New string: hello;dear
Removed: John
Original string: nothing to remove
New string: nothing to remove
Removed: NOTHING

You can also try it live here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文