从字符串的可选点删除一个字符

发布于 2024-10-20 10:14:25 字数 76 浏览 3 评论 0原文

我想从 c lang 中字符串的可选点删除一个字符。我想通过指针和 strcat() 函数编写这个程序。请指导我

谢谢大家

I want to remove a character from an optional point of string in c lang.. I want to write this program via pointers and strcat() function. Please guid me

Thanks all

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

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

发布评论

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

评论(2

油饼 2024-10-27 10:14:25

为什么要使用 strcat() 呢?您所需要的只是 memmove()

void remove_char_at(char *str, unsigned int pos) {
    memmove(str + pos, str + pos + 1, strlen(str) - pos);
}

演示:http://codepad.org/SrgzQohD

Why would you use strcat() for that? All you need is memmove():

void remove_char_at(char *str, unsigned int pos) {
    memmove(str + pos, str + pos + 1, strlen(str) - pos);
}

Demo: http://codepad.org/SrgzQohD

中二柚 2024-10-27 10:14:25

这是我编写的一个小示例程序,用于使用 strcat 从字符串中删除字符。我在评论中解释了步骤。

您可能需要添加一些额外的功能,例如检查 pos >= 0 &&位置 < strlen(字符串)

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

char *removeCharacter(char *string, int pos);

int main(void) {
   char string[] = "Testing strings"; // The string to remove chars from
   char *newString; // The resulting string

   newString = removeCharacter(string, 3);
   printf("Result is '%s'\n", newString); // Print result

   free(newString); // Clean up allocated memory for the resulting string.

   return 0;
}

char *removeCharacter(char *string, int pos) {
   char buffer[255]; // Temporary storage for the beginning of the string
   char *appendix = string + (pos + 1); // Appendix (rest of the string without omitted character)
   char *newString = (char *)malloc(255 * (sizeof(char))); // Allocate some memory for the resulting string

   printf("Copying %d chars from %s to buffer...\n", pos, string);
   strncpy(buffer, string, pos); // Copy pos characters from string to buffer (our beginning of the string)
   buffer[pos] = '\0'; // Don't forget to add a NULL byte to indicate the end of the string

   printf("Buffer is '%s' and appendix is '%s'\n", buffer, appendix);
   strcat(newString, buffer); // Concatenate buffer (beginning) and appendix (ending without character)
   strcat(newString, appendix);

   return newString;
}

Here is a small example program I wrote for removing a character from a string using strcat. I explained the steps in the comments.

You may have to add some extra features such as checking whether pos >= 0 && pos < strlen(string).

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

char *removeCharacter(char *string, int pos);

int main(void) {
   char string[] = "Testing strings"; // The string to remove chars from
   char *newString; // The resulting string

   newString = removeCharacter(string, 3);
   printf("Result is '%s'\n", newString); // Print result

   free(newString); // Clean up allocated memory for the resulting string.

   return 0;
}

char *removeCharacter(char *string, int pos) {
   char buffer[255]; // Temporary storage for the beginning of the string
   char *appendix = string + (pos + 1); // Appendix (rest of the string without omitted character)
   char *newString = (char *)malloc(255 * (sizeof(char))); // Allocate some memory for the resulting string

   printf("Copying %d chars from %s to buffer...\n", pos, string);
   strncpy(buffer, string, pos); // Copy pos characters from string to buffer (our beginning of the string)
   buffer[pos] = '\0'; // Don't forget to add a NULL byte to indicate the end of the string

   printf("Buffer is '%s' and appendix is '%s'\n", buffer, appendix);
   strcat(newString, buffer); // Concatenate buffer (beginning) and appendix (ending without character)
   strcat(newString, appendix);

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