Country
Country

Reputation: 485

Get index of substring

I have char * source, and I want extract from it subsrting, that I know is beginning from symbols "abc", and ends where source ends. With strstr I can get the poiner, but not the position, and without position I don't know the length of the substring. How can I get the index of the substring in pure C?

Upvotes: 37

Views: 76794

Answers (7)

Howard J
Howard J

Reputation: 421

Here is a C version of the strpos function with an offset feature...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int strpos(char *haystack, char *needle, int offset);
int main()
{
    char *p = "Hello there all y'al, hope that you are all well";
    int pos = strpos(p, "all", 0);
    printf("First all at : %d\n", pos);
    pos = strpos(p, "all", 10);
    printf("Second all at : %d\n", pos);
}


int strpos(char *hay, char *needle, int offset)
{
   char haystack[strlen(hay)];
   strncpy(haystack, hay+offset, strlen(hay)-offset);
   char *p = strstr(haystack, needle);
   if (p)
      return p - haystack+offset;
   return -1;
}

Upvotes: 3

StrongLucky
StrongLucky

Reputation: 588

A function to cut a word out out of a string by a start and end word

    string search_string = "check_this_test"; // The string you want to get the substring
    string from_string = "check";             // The word/string you want to start
    string to_string = "test";                // The word/string you want to stop

    string result = search_string;            // Sets the result to the search_string (if from and to word not in search_string)
    int from_match = search_string.IndexOf(from_string) + from_string.Length; // Get position of start word
    int to_match = search_string.IndexOf(to_string);                          // Get position of stop word
    if (from_match > -1 && to_match > -1)                                     // Check if start and stop word in search_string
    {
        result = search_string.Substring(from_match, to_match - from_match);  // Cuts the word between out of the serach_string
    }

Upvotes: 0

glglgl
glglgl

Reputation: 91049

Formally the others are right - substring - source is indeed the start index. But you won't need it: you would use it as index into source. So the compiler calculates source + (substring - source) as the new address - but just substring would be enough for nearly all use cases.

Just a hint for optimization and simplification.

Upvotes: 2

KevinDTimm
KevinDTimm

Reputation: 14376

char *source = "XXXXabcYYYY";
char *dest = strstr(source, "abc");
int pos;

pos = dest - source;

Upvotes: 5

Robert S. Barnes
Robert S. Barnes

Reputation: 40558

Use pointer subtraction.

char *str = "sdfadabcGGGGGGGGG";
char *result = strstr(str, "abc");
int position = result - str;
int substringLength = strlen(str) - position;

Upvotes: 72

Graham Borland
Graham Borland

Reputation: 60681

If you have the pointer to the first char of the substring, and the substring ends at the end of the source string, then:

  • strlen(substring) will give you its length.
  • substring - source will give you the start index.

Upvotes: 2

Matt K
Matt K

Reputation: 13852

newptr - source will give you the offset.

Upvotes: 6

Related Questions