[英]Converting modified strtok tokens back into the original string's “syntax”
Is there a way after you've split a string into tokens to convert these tokens back into the original string (with punctuation, and original case-sensitivity)? 将字符串拆分为标记后,是否有办法将这些标记转换回原始字符串(具有标点符号和原始区分大小写)?
I'm fairly certain strcat will be invoked, but what would be a good method to maintain the original string's punctuation & case-sensitivity? 我相当确定会调用strcat,但是什么是维护原始字符串的标点符号和区分大小写的好方法?
Assume that I need to modify each and every token individually before concatenating them back into the original string's structure. 假设我需要分别修改每个令牌,然后再将它们串联回到原始字符串的结构中。
Edit: The title has been reworded & clarified, I hope. 编辑:我希望标题已经改写并澄清了。
char* msg = malloc(10000);
char* msg2 = malloc(10000);
char* buffer;
char buffer2[10000];
printf("input: \n");
fgets(msg, 9999, stdin);
msg2 = strdup(msg);
buffer = strtok(msg2, " ;,.!?\n");
Node* ptr; // assume this is initialized to a linked list containing 2 strings per node (str, str2)
while (buffer) {
for (int i = 0; i < strlen(buffer); i++) { //strcmp is case-sensitive
if (isalpha(buffer[i])) {
buffer[i] = tolower(buffer[i]);
}
}
while (ptr) {
if ((ptr != NULL) && (strcmp(ptr->str, ptr->str2) != 0)) {
buffer = ptr->str2; // modifies the token
break;
}
}
ptr = ptr->next;
}
strcat(buffer2, " "); // this will concatenate the modified tokens back together with spaces
strcat(buffer2, buffer); // but I lose the original string's structure (i.e.: punctuation, case-sensitivity)
buffer = strtok(NULL, " ;,.!?\n");
printf("%s", buffer2); // so how should I get the original string's structure back with the modified tokens?
If you want to use strtok()
and want to access the original string later, please keep a copy of the original string, since strtok() will destroy the original string. 如果要使用
strtok()
并希望以后再访问原始字符串,请保留原始字符串的副本,因为strtok()会破坏原始字符串。
// we have char *st ;
char *tem = malloc(strlen(st)+1);
strcpy(tem, st);
Now you can access tem as original string later. 现在,您以后可以将tem作为原始字符串访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.