Using traversal by pointer to check whether a string is repeated

Posted by Bob John on Stack Overflow See other posts from Stack Overflow or by Bob John
Published on 2012-10-24T04:09:21Z Indexed on 2012/10/24 5:02 UTC
Read the original article Hit count: 104

Filed under:
|
bool repeat_char(char *s, int n);
//R: s is a C-string of at least n non-NUL characters and n > 0
//E: returns true if the first n characters are fully repeated throughout the string s, false
//   otherwise.

I'm having trouble implementing this function using traversal by pointer. I was thinking that I could extract the first n characters from s, then use that in a comparison with s, but I'm not sure how I could do that. If I'm traversing through s one character at a time, how can I check that it matches a block of text, such as the first n characters of s?

Thanks!

© Stack Overflow or respective owner

Related posts about c++

Related posts about pointers