to_lower function entering an infinite loop

Started by
27 comments, last by MarcusAseth 6 years, 7 months ago
11 hours ago, MarcusAseth said:

@h8CplusplusGuru much more clean/compact than mine, regardless if I had done it without the const char required by the exercise, thanks for showing me

 

 

Except that code doesn't find "abac" in the string "ababac". Correctness matters.

 

Advertisement

lol xD 

Edit: since my lol above was downvoted, I realize it is kind of ambigous and possibly gives the wrong impression of what I laughed about, to put it in context: : it was because the other day I was saying to @h8CplusplusGuru that in my opinion the benefit of the forum was that anyone can correct each other and be corrected so that who's learning has more chance to get the correct informations. My lol was on the lines of "I knew it!"/"I said it"- don't hate pls :D

 

 

On 9/14/2017 at 10:45 AM, alvaro said:

 

Except that code doesn't find "abac" in the string "ababac". Correctness matters.

 

I thought it was odd that there was only one loop!  Regardless, it was my intention to show that he's mostly poking around in the dark.

On 9/14/2017 at 10:59 AM, MarcusAseth said:

lol xD 

Edit: since my lol above was downvoted, I realize it is kind of ambigous and possibly gives the wrong impression of what I laughed about, to put it in context: : it was because the other day I was saying to @h8CplusplusGuru that in my opinion the benefit of the forum was that anyone can correct each other and be corrected so that who's learning has more chance to get the correct informations. My lol was on the lines of "I knew it!"/"I said it"- don't hate pls :D

I think the first line of what I had said was that forums such a gamedev are always around.


//take two char* (strings) and return char* to first occurence of second string
char* findx(char* str, char* strx ) {
	
	if (!str || !strx) return nullptr;


	for (int s = 0; str[s] != '\0'; s++) {

		if (str[s] != strx[0]) continue;

		char* found = &str[s];

		int x = 0;
		for ( x=1; strx[x] != '\0' && str[s + x] != '\0'; x++) {
			if (str[s + x] != strx[x]) 
				break;
		}
		if (strx[x] == '\0') return found;
	}

	return nullptr; 
}

Anyways, If you ever need a flashlight =)

1 hour ago, h8CplusplusGuru said:

I thought it was odd that there was only one loop!  Regardless, it was my intention to show that he's mostly poking around in the dark.

Well it wasn't odd, you could do it with a single loop, I just did it out of curiosity to discover if I could do it. Seems I can :D
Let me know if you find any logic bug though, just to be sure this is viable.


char* findx(char* s, char* x)
{
	if (s == nullptr || x == nullptr) { return nullptr; }

	int Incremented = 0;
	for (; *s != '\0'; s++)
	{
       		if (*x == '\0') { break; }
		if (*s == *x)
		{
			x++;
			Incremented++;
			continue;
		}
		else
		{
			x -= Incremented;
			s -= Incremented;
			Incremented = 0;
		}
	}
	return (*x == '\0') ? s -= Incremented : nullptr;
}

 

As a matter of style, I wouldn't check if the inputs are nullptr. Just like with standard-library functions, it's your responsibility not to pass null pointers around.

However, searching for the empty string should return the first location where that string can be found as a substring, which should be the first character.

Although the new code by h8CplusplusGuru is better, I think it doesn't do the right thing if you look for the empty string.

35 minutes ago, alvaro said:

 I think it doesn't do the right thing if you look for the empty string.

I actually tested @h8CplusplusGuru  code with your previous parameters findx("ababac", "abac") and it return nulltpr, so something else is wrong beside passing an empty string :/

1 hour ago, MarcusAseth said:

I actually tested @h8CplusplusGuru  code with your previous parameters findx("ababac", "abac") and it return nulltpr, so something else is wrong beside passing an empty string :/

Hmmm... I'm not sure how you tried, but it seems to work for me.

2 minutes ago, alvaro said:

Hmmm... I'm not sure how you tried, but it seems to work for me.

My bad, I just realized I had scrolled past the latest example and compiled the first (old) example x_x

I'm fully aware it can be done with a single loop, lol. My thought was that It didn't check previous values in the array, most easily solved with a second loop rather than contriving something with a single loop.


char* findx(char* str, char* strx)
{
	if (str == nullptr || strx == nullptr) { return nullptr; }

	char* found = nullptr;
	int s = 0, x = 0;

	while (str[s] != '\0') {

		if (!found) found = &str[s];
		if (strx[x] == '\0') return found;

		if (str[s] != strx[x]) {
			found = nullptr;
			s = s-x +1;
			x = 0;
		} else {
			s++;
			x++;
		}
	}

	return nullptr;
}

 

 

 

@h8CplusplusGuru  personally I like this version better for readability, I wonder if there is a way to time it (since it run probably too fast) to see if is better performant than the double loop version.

Only thing is it should return found instead of nullptr at the end :P

 

This topic is closed to new replies.

Advertisement