Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C substrings / C string slicing?

Tags:

c

Hy everybody! I am trying to write a program that checks if a given string of text is a palindrome (for this I made a function called is_palindrome that works) and if any of it's substrings is a palindrome, and I can't figure out what is the optimal way to do this:

For example, for the string s = "abcdefg" it should first check "a", then "ab", "abc", "abcd" and so on, for each character

In Python this is the equivalent of
s[:1], s[:2], ...      (a, ab, ...)
s[1:2], s[1:3] ...     (b, bc, ...)

What function/method is there that I can use in a similar way in C ?

like image 695
guest1 Avatar asked Oct 28 '14 23:10

guest1


People also ask

Is string slicing possible in C?

Slicing Strings with strsep() The strtok() function is the traditional C library routine used to slice up a string. It has its limitations, however, so a more recent function was included in the library on some compilers to replace it: strsep().

How do you check if a substring is present in a string in C?

In this C program, we are reading a string using gets() function 'str' character variable. We are reading value another string to search using search character variable. To check substring is present in the given string. While loop is used to compute the str[] and search[] array variable value is not equal to null.

Does C# have string slicing?

To "slice" a string, you use the Substring method: string word = "hello"; string ordw = word. Substring(1) + word. Substring(0, 1);

Can slicing be done on strings?

Slicing StringsYou can return a range of characters by using the slice syntax. Specify the start index and the end index, separated by a colon, to return a part of the string.


2 Answers

This slice_str() function will do the trick, with end actually being the end character, rather than one-past-the-end as in Python slicing:

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

void slice_str(const char * str, char * buffer, size_t start, size_t end)
{
    size_t j = 0;
    for ( size_t i = start; i <= end; ++i ) {
        buffer[j++] = str[i];
    }
    buffer[j] = 0;
}

int main(void) {
    const char * str = "Polly";
    const size_t len = strlen(str);
    char buffer[len + 1];

    for ( size_t start = 0; start < len; ++start ) {
        for ( int end = len - 1; end >= (int) start; --end ) {
            slice_str(str, buffer, start, end);
            printf("%s\n", buffer);
        }
    }

    return 0;
}

which, when used from the above main() function, outputs:

paul@horus:~/src/sandbox$ ./allsubstr
Polly
Poll
Pol
Po
P
olly
oll
ol
o
lly
ll
l
ly
l
y
paul@horus:~/src/sandbox$ 
like image 119
Crowman Avatar answered Sep 20 '22 23:09

Crowman


This is the one liner I use to get a slice of a string in C.

void slice(const char *str, char *result, size_t start, size_t end)
{
    strncpy(result, str + start, end - start);
}

Pretty straightforward. Given you've checked boundaries and made sure end > start.

like image 34
Etsitpab Nioliv Avatar answered Sep 20 '22 23:09

Etsitpab Nioliv