-1

So I've got this here:

#include <stdio.h>

char halloString[] = "Ha::ll::o";
char perfumeString[] = "47::11";
char veryLongString[] = "47::11::GHesd::dghsr::bfdr:hfgd46dG";

char *extract (char *input) {somethinghappenshere}

where extract needs to get all characters after the last double ":" of given input:

  • "o" for halloString
  • "11" for perfumeString
  • "bfdr:hfgd46dG" for veryLongString

In short, my issue is finding the length of the string *input points to. As far as I understand it that won't be happening without making something really sketchy.

Am I correct in assuming the length cannot be acquired in a good way?

And if so would it be a horrible idea to do, for example:

char stringToProcessTemp1[50];
char stringToProcessTemp2[50];
char stringToProcess[50];
for (int i = 0; i < 50; i++) {
  stringToProcessTemp1[i] = input + i;
}
for (int i = 0; i < 50; i++) {
  stringToProcessTemp2[i] = input + i;
}
for (int i = 0; i < 50; i++) {
  if (stringToProcessTemp1[i] == stringToProcessTemp2[i]) {
    stringToProcessTemp[i] = stringToProcessTemp1[i];
  }
}

Later checking where the first empty index is and saving everything before it as the used String as from my very limited experience in C when you go outside of an array you tend to get different outputs every time therefore making the chance both Temp strings match for an extra element directly after the last one of the original string what I'd consider low enough.

It's honestly the only idea I've got right now.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
  • 3
    Is your question, how do you find the last occurrence of `"::"` or how do you find the length of a string? – Fiddling Bits Nov 08 '18 at 16:34
  • Possible duplicate of [Finding the length of a Character Array in C](https://stackoverflow.com/questions/4180818/finding-the-length-of-a-character-array-in-c) – AmourK Nov 08 '18 at 16:34
  • 1
    you can use [`strlen`](https://linux.die.net/man/3/strlen) in this case since you're working with strings. – yano Nov 08 '18 at 16:34
  • You're talking about the "chance" of strings matching when you access past the end of the array. This is a very dangerous mindset; you need to understand that accessing past the end of an array is Undefined Behavior and the standard says that *anything* can happen. – Jonathon Reinhart Nov 08 '18 at 16:36
  • This seems like an XY question (rather than an XY problem, rather than just a problem). @FiddlingBits is reading the problem as I did; it seems you need a way to back-walk your string looking for separators and assembling your results along the way. Either that or we're both suffering from the same delusion (a distinct possibility). – WhozCraig Nov 08 '18 at 16:38
  • http://man7.org/linux/man-pages/man3/strtok_r.3.html might be of use – Milos Matovic Nov 08 '18 at 16:47
  • @FiddlingBits It's about the length of a string. Thought I should add the "::" part just to give some context as to the point of the exercise. I appreciate the help. I'll try making two versions with strlen and strstr to see which makes more sense in my head. – Lanolderen Nov 08 '18 at 17:27

2 Answers2

1

Finding the length of a string is no problem. strlen will do that for you. However, you don't even need that.

You can use the strstr function to find a substring within a string, in this case "::". When you find one, keep looking right after the last one you found until you don't find it anymore, then the last one you found is the one you want. Then you want the substring that starts right after it.

char *extract(char *input)
{
    char *last = NULL, *start = input, *curr;
    while ((curr == strstr(start, "::")) != NULL) {
        last = curr;       // keep track of the last "::" found
        start = last + 1;  // move the starting string to right after the last "::"
                           // move up 1 instead of 2 in case of ":::"
    }
    if (last != NULL) {
        last +=2;     // We found one; move just past the "::"
    }
    return last;
}
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Only problem with this would be if the string ends with "::" it would return a pointer to some memory that might be anything. But i guess it doesn't really matter for OP – Milos Matovic Nov 08 '18 at 17:13
  • Thank you for mentioning strstr. I will try making two solutions with strlen and strstr to see which clicks better with me. @MilosMatovic It shouldn't be a problem but thank you for mentioning it as well. If I have the time I'll add a string that ends in "::" and try to fix it as an extra exercise. – Lanolderen Nov 08 '18 at 17:35
  • @MilosMatovic In that case it returns a pointer to the null terminator at the end of the string, i.e. an empty string. – dbush Nov 08 '18 at 17:36
0

C strings, which are really only an array of characters, are by definition terminated by '\0'. So, for a well formed C string you can always get the length of the string by using strlen().

If, however, your string is not null-terminated, there is no way to determine it's length, and it is not a C string by definition any more, but just an array of characters.

GermanNerd
  • 643
  • 5
  • 12
  • If the string has not been correctly formated, it can be missing the trailing null byte. I that case the condition read (`if pointer[i]`) read will throw a warning, and read/write leads to *[undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior)* or **Segmentation Fault (Core Dumped)** – Jean-Marc Zimmer Nov 08 '18 at 16:39
  • "C strings, ... are by definition terminated by '\0'" and "If, ..., your C string is not null-terminated," contradicts. For clarity: a character array may lack a _null character_. In C, a _string_ always has a _null character_. – chux - Reinstate Monica Nov 08 '18 at 16:42
  • @chux Haha, language lawyer? I'll edit my post. Thanks. – GermanNerd Nov 08 '18 at 16:47