So, I have been looking over this function and I am not really sure how i would actually write it out. I have the plan, just need the syntax.
1 2 3 4 5 6 7
bool repeat_char(char *s, int n);
// Requires: S is a C-string of at least n non-NUL characters
// Effects: Returns true if the first n characters are fully
// repeated throughout the string s, otherwise false
// Examples: repeat_char("123123", 3) == true
// repeat_char("abcdeabcdeabcdeabcdeabcde", 5) == true
// repeat_char("12312312", 3) == false
(Only traversal by pointer, no array subscripting)
So my plan is to first make a temp C-string/array of characters, that is equal to the first n of s. Then have a while loop, while(*s).
Inside the while loop, I plan on having a for loop that checks the character in chunks of n, to see if the characters are fully repeated. This part is hard to explain, here is what i mean.
the plan is solid, as long as your "temp C-strnig/array" is in fact a subrange within the array you got, that is, a pair of pointers [s, s+n), not a physical copy of the array.
then you can indeed to as you planned -- there's a lot of ways to write this.
So I've looked through my code and refined it a bit. Sadly, we can't use regex on the exam :/
In the 2nd chunk of code you gave. I don't really understand the majority of it. I think I'd rather try to work off of my code. I've fixed it a bit, and now am just getting a compiler error on line 5. "Expression must have a constant value." Which is why i tried
"Expression must have a constant value." refers to your attempt to create an array whose size (n) is not known until the function is called. You could use string first_n(s, s+n);or vector<char> first_n(s, s+n);
but it's important to see why you don't need this at all: you already have the array, it's the first n characters of s. If you want to access that array, you just need two pointers:
1 2
constchar* first_n_beg = s;
constchar* first_n_end = s + n;
So I get what you mean about having 2 pointers. But I don't understand. Could someone take the time to walk me through this?
1 2 3 4 5 6 7 8
bool repeat_char(constchar* s, size_t n)
{
constchar* p = s + n;
while(*++p)
if(*p != s[(p-s) % n])
returnfalse;
return (p-s) % n == 0;
}
const char* p = s + n; This will make p point to the last part of what we have to check. (To see if it repeats)
while(*++p) If I didn't know this was right, I would have thought it was wrong. If you are on the last character in "123123123", the last 3. wouldn't the while loop evaluate "\0" instead of 3? But then again, if it pre-incremented it, the last 3 would have already gotten evaluated . Bahhh this confuses me, I feel like this misses the first or last character
if(*p != s[(p-s) % n]) No clue
return false;
return (p-s) % n == 0; No clue either. I think this checks to see if there are any left over? Not sure what (p-s) means/is/signifies/why
" 1 2 3 1 2 3 1 2 3 " n = 3
|-----------|--------------------
( s + n = p )
(p-s) is the distance between p and s, 3 in the above scenario. % is the modulus operator and returns the remainder of division. 3 / 3 = 0, remainder is 0. if(*p != s[0]).
Modulus is commonly used to limit the range of a value, n % 3 will result in a value of either 0, 1 or 2. So, no matter the distance between p and s, the index will be in range of the first 3 characters.
Yezman wrote:
return (p-s) % n == 0; No clue either.
p now points to the end of the string, and s has always pointed to the beginning. The end minus the beginning is the length of the string (excluding null). (length % n) == 0 ensures the string is a multiple of the pattern size. Without this test, "12312312" would return true.
Yezman wrote:
while(*++p) If I didn't know this was right, I would have thought it was wrong.
That is a minor mistake Cubbi made, incrementing p before the first comparison. Replace while(*++p) with for(; *p; ++p).
bool repeat_char(const char* s, size_t n)
{
const char* p = s + n;
while(*++p)
if(*p != s[(p-s) % n])
return false;
return (p-s) % n == 0;
}
how about repeat_char("a23123",3)?
while(*++p) should be while(*p++)
you forgot the differences between p++ and ++p.