tags:

views:

64

answers:

2

Is there a function to get a range of characters in a text string or would i need make my own?

+1  A: 

something like this might help

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

main(){
  const char* from = "12345678";
  char *to = (char*) malloc(6);
  strncpy(to, from+2, 5);
}

EDIT: source

Gabriel Sosa
http://www.linuxquestions.org/questions/programming-9/extract-substring-from-string-in-c-432620/
Snake Plissken
A: 

Nothing standard, but extremely easy to write:

char *char_range(char begin, char end)
{
    size_t len = end - begin + 1;
    char *arr = malloc(len + 1);
    if (arr)
    {
        size_t i;
        for (i = 0; i < len; ++i)
            arr[i] = (char)(begin + i);
        arr[len] = '\0';
    }
    return arr;
}

Because this generates the range based on the ordinal value of characters, technically this is platform specific. That said, since ASCII and Unicode are pretty much the lingua-franca of most modern OS's these days, you can calls such as:

char *lower_case = char_range('a', 'z');
char *numbers = char_range('0', '9');
R Samuel Klatchko