如何用C中的strtok删除字符数组中的最后一个字符串?
我有一个函数,它接受一些值作为如何用C中的strtok删除字符数组中的最后一个字符串?,c,arrays,strtok,C,Arrays,Strtok,我有一个函数,它接受一些值作为char数组[]参数 这些值用分号(;”)分隔 例如:“你好;亲爱的;约翰” 因此,我试图找到一种方法,使用strtok删除最后一个字符串,即最后一个分号后面的“John” int remove(char lastName[]){ } *更具体地说 我创建了此函数,用于删除以分号分隔的值: int remove(char variable_Name[]){ char *value_toRemove = getenv(variable_Name
char数组[]
参数
这些值用分号(;”
)分隔
例如:“你好;亲爱的;约翰”
因此,我试图找到一种方法,使用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;
}
但函数在找到分号后会删除所有内容。
strrchr
将查找该字符的最后一次出现
int remove(char lastName[]){
}
Sor如果您不介意修改原始字符串,那么它应该像
int remove(char *lastName){
char *pos = strrchr(lastName, ';');
if (pos) {
*pos = 0;
return pos-lastName;
}
return 0;
}
手册页
strrchr
将查找该字符的最后一次出现
Sor如果您不介意修改原始字符串,那么它应该像
int remove(char *lastName){
char *pos = strrchr(lastName, ';');
if (pos) {
*pos = 0;
return pos-lastName;
}
return 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;
}
你也可以在现场试一试
编辑:对于您的评论,它确实有效。我就是这样做的,我已经包括了整个程序,以显示输出的示例:
#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;
}
您也可以现场试用。ty获取快速答案。我还想问你我的函数应该返回什么?@user1031654如果
last_semi
是NULL
(”;“
在字符串中找不到),我可能会返回0(即FALSE),并返回1(即TRUE)或任何其他非零值以指示”;”已找到代码>,并替换为'\0'
。甚至可能在被切断的字符串末尾添加一个char*
。如果它对您不重要,那么您可以将函数的返回类型设置为void
。不幸的是,它似乎不起作用:/strtok有什么办法吗?@user1031654它确实起作用,我已经编辑了我的答案,并显示了整个函数。我已经修改了您的代码以接受来自stdin的输入,从而允许在不修改代码的情况下更改输入。为了快速的回答,我需要你的帮助。我还想问你我的函数应该返回什么?@user1031654如果last_semi
是NULL
(”;“
在字符串中找不到),我可能会返回0(即FALSE),并返回1(即TRUE)或任何其他非零值以指示”;”已找到代码>,并替换为'\0'
。甚至可能在被切断的字符串末尾添加一个char*
。如果它对您不重要,那么您可以将函数的返回类型设置为void
。不幸的是,它似乎不起作用:/strtok有什么办法吗?@user1031654它确实起作用,我已经编辑了我的答案,并显示了整个函数。我已经修改了您的代码以接受来自stdin的输入,从而允许在不修改代码的情况下更改输入。你能告诉我*pos=0;返回pos lastName;do?它通过简单计算字符串开头的指针(lastName)和我们调零的位置(pos)之间的差来返回新截断字符串的长度。。两个指针的减法给出了它们之间的字节数。你能告诉我*pos=0;返回pos lastName;do?它通过简单计算字符串开头的指针(lastName)和我们调零的位置(pos)之间的差来返回新截断字符串的长度。。两个指针的减法给出它们之间的字节数。