C - 子字符串(从 POS 到 POS)

发布于 2024-12-22 20:38:02 字数 381 浏览 0 评论 0原文

我有一个长度为 32 的字符数组,想从中取出某些字符。 例如

1111110000000000000000000111111 <32个字符

我想采用字符0-6,这将是111111

或者甚至采用字符26-31,这将是111111 code>

char check_type[32];

以上是我的声明方式。

我希望能够做的是定义一个函数或使用一个占据起始位置和结束字符的函数。

我已经研究了很多方法,例如使用 strncpystrcpy 但还没有找到方法。

I have a character array of length 32 and would like to take certain charcters out of it.
for example

111111000000000000000000111111 <32 chars

I would like to take chars 0-6 which would be 111111

Or even take chars 26-31 which would be 111111

char check_type[32];

Above is how I'm declaring.

What I would like to be able to do is define a function or use a function that takes that starting place, and end character.

Ive looked at many ways like using strncpy and strcpy but found no way yet.

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

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

发布评论

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

评论(5

南街九尾狐 2024-12-29 20:38:02

我会简单地包装 strncpy:

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

/* Creates a sub-string of range [start, end], return value must be freed */
char *substr(char *src, size_t start, size_t end)
{
   size_t sub_len = end - start + 1;
   char * new_str = malloc(sub_len + 1); /* TODO: check malloc's return value */

   strncpy(new_str, src, sub_len);
   new_str[sub_len] = '\0'; /* new_str is of size sub_len + 1 */

   return new_str;
}

int main(void)
{
   char str[] = "111111000000000000000000111111";
   char *sub_str = substr(str, 0, 5);

   puts(sub_str);

   free(sub_str);
   return EXIT_SUCCESS;
}

输出:

111111

I would simply wrap strncpy:

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

/* Creates a sub-string of range [start, end], return value must be freed */
char *substr(char *src, size_t start, size_t end)
{
   size_t sub_len = end - start + 1;
   char * new_str = malloc(sub_len + 1); /* TODO: check malloc's return value */

   strncpy(new_str, src, sub_len);
   new_str[sub_len] = '\0'; /* new_str is of size sub_len + 1 */

   return new_str;
}

int main(void)
{
   char str[] = "111111000000000000000000111111";
   char *sub_str = substr(str, 0, 5);

   puts(sub_str);

   free(sub_str);
   return EXIT_SUCCESS;
}

Output:

111111
无畏 2024-12-29 20:38:02

使用memcpy。

// Stores s[from..to) in sub.
// The caller is responsible for memory allocation.
void extract_substr(char const *s, char *sub, size_t from, size_t to)
{
    size_t sublen = to - from;
    memcpy(sub, s + from, sublen);
    sub[sublen] = '\0';
}

Use memcpy.

// Stores s[from..to) in sub.
// The caller is responsible for memory allocation.
void extract_substr(char const *s, char *sub, size_t from, size_t to)
{
    size_t sublen = to - from;
    memcpy(sub, s + from, sublen);
    sub[sublen] = '\0';
}
怕倦 2024-12-29 20:38:02

示例:

char *substr(char *source, int startpos, int endpos)
{
    int len = endpos - startpos + 2; // must account for final 0
    int i = 0;

    char *src, *dst;
    char *ret = calloc(len, sizeof(char));

    if (!ret)
        return ret;

    src = source + startpos;
    dst = ret;

    while (i++ < len)
        *dst++ = *src++;

    *dst = 0;
    return ret;
}

当然,当您不再需要返回代码时,请释放它。您会注意到此函数不会检查 endposstartpos 的有效性。

Sample:

char *substr(char *source, int startpos, int endpos)
{
    int len = endpos - startpos + 2; // must account for final 0
    int i = 0;

    char *src, *dst;
    char *ret = calloc(len, sizeof(char));

    if (!ret)
        return ret;

    src = source + startpos;
    dst = ret;

    while (i++ < len)
        *dst++ = *src++;

    *dst = 0;
    return ret;
}

Of course, free the return code when you don't need it anymore. And you notice this function will not check for the validity of endpos vs startpos.

儭儭莪哋寶赑 2024-12-29 20:38:02

首先定义所需的接口...也许:

int substring(char *target, size_t tgtlen, const char *source, size_t src_bgn, size_t src_end);

这需要一个将复制数据的目标(目标)数组,并给出其长度。数据将来自源数组,位置 src_bgnsrc_end 之间。如果出现错误,则返回值为 -1,并且返回输出的长度(不包括终止 null)。如果目标字符串太短,则会出现错误。

有了这组细节,您就可以相当轻松地实现主体,并且 strncpy() 这次可能很合适(通常不合适)。

用法(根据您的问题):

char check_type[32] = "111111000000000000000000111111";
char result1[10];
char result2[10];

if (substring(result1, sizeof(result1), check_type, 0, 6) <= 0 ||
    substring(result2, sizeof(result2), check_type, 26, 31) <= 0)
    ...something went wrong...
else
    ...use result1 and result2...

First define the required interface...perhaps:

int substring(char *target, size_t tgtlen, const char *source, size_t src_bgn, size_t src_end);

This takes a destination (target) array where the data will be copied, and is given its length. The data will come from the source array, between positions src_bgn and src_end. The return value will be -1 for an error, and the length of the output (excluding the terminating null). If the target string is too short, you will get an error.

With that set of details in place, you can implement the body fairly easily, and strncpy() might well be appropriate this time (it often isn't).

Usage (based on your question):

char check_type[32] = "111111000000000000000000111111";
char result1[10];
char result2[10];

if (substring(result1, sizeof(result1), check_type, 0, 6) <= 0 ||
    substring(result2, sizeof(result2), check_type, 26, 31) <= 0)
    ...something went wrong...
else
    ...use result1 and result2...
倒带 2024-12-29 20:38:02

检查一下:

char* Substring(char *string, int len, int start, int end) {
  /*
  Creates a substring from a given string.
  Args:
    string: The string whose substring you need to find.
    len: The length of the string.
    start: The start position for the substring.
    end: The end position of the substring (inclusive).
  Returns:
    substring: (of type char*) which is allocated on the heap.
    NULL: on error.
  */

  // Check that the start and end position are valid.
  // If not valid, then return NULL.
  if (start < 0 || start >= len || end < 0 || end >= len) {
    return NULL;
  }

  // Allocate memory to return the substring on the heap.

  char *substring = malloc(sizeof(char) * (end - start + 2));
  int index = 0, i;

  for (i = start; i <= end; i++) {
    substring[index] = string[i];
    index++;
  }

  // End with a null character.
  substring[index] = '\0';
  return substring;
}

int main() {
  char str[] = "11111100000000000000000000111111";
  printf("%s\n", Substring(str, strlen(str), 0, 5));
  printf("%s\n", Substring(str, strlen(str), 26, 31));
}

Check this:

char* Substring(char *string, int len, int start, int end) {
  /*
  Creates a substring from a given string.
  Args:
    string: The string whose substring you need to find.
    len: The length of the string.
    start: The start position for the substring.
    end: The end position of the substring (inclusive).
  Returns:
    substring: (of type char*) which is allocated on the heap.
    NULL: on error.
  */

  // Check that the start and end position are valid.
  // If not valid, then return NULL.
  if (start < 0 || start >= len || end < 0 || end >= len) {
    return NULL;
  }

  // Allocate memory to return the substring on the heap.

  char *substring = malloc(sizeof(char) * (end - start + 2));
  int index = 0, i;

  for (i = start; i <= end; i++) {
    substring[index] = string[i];
    index++;
  }

  // End with a null character.
  substring[index] = '\0';
  return substring;
}

int main() {
  char str[] = "11111100000000000000000000111111";
  printf("%s\n", Substring(str, strlen(str), 0, 5));
  printf("%s\n", Substring(str, strlen(str), 26, 31));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文