【问题标题】:HOW do i delete a symbol from a char array(not string array)我如何从字符数组(不是字符串数组)中删除符号
【发布时间】:2020-05-21 16:02:26
【问题描述】:

我有一个客户端-服务器程序,其中客户端发送一个字符数组,如果数组的长度是偶数,服务器会删除 3 个第一个和 2 个最后一个符号(不包括 0 终止符)并将该数组发送回客户端

问题是如何从字符数组中删除特定符号(我实际上并不擅长使用指针,我想在这里使用它们可能是一个解决方案) 这是有问题的代码部分:

 while (recv(s, b, sizeof(b), 0) != 0) //while connection is open
    {
        l = strlen(b); 
        if (l%2==0)//if length is even
        {
            for (i = j = 0; i < l; i++) 
            {
                ch = b[i]; //current symbol
                if (i!=0 || i!=1 || i!=2 || i!=l-1 || i!=l-2)
                {
                    b[j++] = ch; // if the symbol fots the condition
                }
            }


        }
        else
        {
            strcpy_s(b, "The string is odd ");
        }
        send(s, b, sizeof(b), 0); //sending the result
    }

【问题讨论】:

  • 你能提供之前/之后你想要的行为的例子吗?
  • 发送:qazwsxedc1 接收:wsxed
  • 当代码明显是 C 时,为什么这会被标记为 C++。在 C++ 中,你会以非常不同的方式执行此操作。
  • @Sasha "thats cpp" - 我不同意。首先,您通常不会在 C++ 中使用 C 样式的数组。你会使用std::arraystd::vector。此外,您可能不会使用sizeof,因为您不需要,如果您想知道某个容器中的元素数量,您可以使用.size()std::size。这段代码看起来像 C,恰好可以用 C++ 编译器构建。
  • 从字符数组中“删除符号”是什么意思? n 字符数组是 n 字符数组。您可以在给定位置更改 char 的值,但删除它并没有多大意义。你可以做什么将指针向前移动 m-chars 并假装数组比实际短 l-chars。就是这样。看起来像是指针章节的灰尘。

标签: c++ arrays char client-server


【解决方案1】:

前两个代码示例假定在修剪发生后不需要原始 C 字符串。只是因为这样更容易触摸。最后一个示例保持原始字符串不变,但如果您只关心更新后的值,则可以将它们分配给自己。

“C++”

#include <cstring>
#include <iostream>

void trim(char buf[], int size) {
  if (size > 5) {
    std::strncpy(buf, &buf[3], size - 5);
    buf[size - 6] = '\0';  // sizeof() counts the null character
  }
}

int main() {
  char too_small[] = "abc";       // Expect same back
  char small[] = "abcdefgh";      // Expect def
  char example[] = "qazwsxedc1";  // Expect wsxed

  trim(too_small, sizeof(too_small));
  trim(small, sizeof(small));
  trim(example, sizeof(example));

  std::cout << too_small << '\n' << small << '\n' << example << '\n';
}

C(你的 C++ 编译器编译它,但你的代码是 C)

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

void trim(char buf[], int size) {
  if (size > 5) {
    strncpy(buf, &buf[3], size - 5);
    buf[size - 6] = '\0';  // sizeof() counts the null character
  }
}

int main() {
  char too_small[] = "abc";       // Expect same back
  char small[] = "abcdefgh";      // Expect def
  char example[] = "qazwsxedc1";  // Expect wsxed

  trim(too_small, sizeof(too_small));
  trim(small, sizeof(small));
  trim(example, sizeof(example));

  printf("%s\n%s\n%s\n", too_small, small, example);
}

C++

#include <iostream>
#include <string>

std::string trim(std::string str) {
  return str.size() > 5 ? str.substr(3, str.size() - 5) : str;
}

int main() {
  std::string too_small("abc");       // Expect same back
  std::string small("abcdefgh");      // Expect def
  std::string example("qazwsxedc1");  // Expect wsxed

  std::cout << trim(too_small) << '\n'
            << trim(small) << '\n'
            << trim(example) << '\n';
}

另一个答案可能比这些示例运行效率更高,但我想发布主要是为了强调 C 和 C++ 之间的一些显着差异,以及如何在两种语言之间以非常不同的方式解决问题。

【讨论】:

  • 谢谢,这些代码示例对我帮助很大!
【解决方案2】:

这是一个简单的程序,演示如何处理字符数组。

#include "stdio.h"
#include "string.h"

int main() {

        /* our input string */
        char *in = "qazwsxedc1";

        /* declare storage for our output string and initialise it */
        char out[500];
        memset(out, 0, 500);

        /* get length of input string */
        unsigned short len = strlen(in);

        /* copy from input string to output */
        strncpy(out, in + 3, len - 3 - 2);

        /* prove that the data were copied */
        printf("Input: %s\n", in);
        printf("Output: %s\n", out);

        /* terminate program */
        return 0;

}

诀窍不是从数组中删除字符,而是选择您想要的字符。未来改进:检查输入字符串是否超过 5 个字符。 (如果不是,您会将垃圾复制到输出字符串中。)

【讨论】:

    猜你喜欢
    • 2020-08-29
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多