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

时间:2011-12-25 22:28:28

标签: c function positioning substring

我有一个长度为32的字符数组,并希望从中获取某些字符。 例如

111111000000000000000000111111< 32 chars

我想采用0到6的字符111111

甚至可以使用字符26-31 111111

char check_type[32];

以上是我宣布的方式。

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

我看过很多方法,比如使用strncpystrcpy,但还没找到。

5 个答案:

答案 0 :(得分:1)

我只需要包裹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

答案 1 :(得分:0)

样品:

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的有效性。

答案 2 :(得分:0)

使用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';
}

答案 3 :(得分:0)

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

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

这将获取将复制数据的目标(目标)数组,并给出其长度。数据将来自位置src_bgnsrc_end之间的源数组。对于错误,返回值将为-1,以及输出的长度(不包括终止空值)。如果目标字符串太短,您将收到错误。

有了这组细节,你可以相当容易地实现身体,这次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...

答案 4 :(得分:0)

检查一下:

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));
}