# From C to Capable -- use the pointer as a function parameter to find out whether the string is a palindrome character

catalogue

π Title:

π‘ Solution ideas:

π code implementation

β¨ code annotation

# π Title:

Enter a string to judge whether it is a palindrome string. Pointer is required as a parameter.

Palindrome string refers to the string with the same forward traversal and reverse traversal.

β­ Example 1:

Input: string = "ABBA"

Output: palindrome string or True

β­ Example 2:

Input: string = "fog lock mountain lock fog"

Output: palindrome string or True

β­ Example 3:

Input: string = "Hello word"

Output: not palindrome number or False

Explanation: forward traversal of Hello word= drow olleH

Palindrome string judgment is rarely used in practice, and it is more common as an exercise.

# π‘ Solution ideas:

According to the definition of palindrome number, forward traversal and reverse traversal are the same string.

Then we can use pointer S to point to the first character of the string. We call pointer S the first pointer, and then pointer E points to the last character of the string. Pointer E is called the tail pointer.

Then we compare whether the characters pointed to by the two pointers are the same. If they are different, the string is directly output instead of the palindrome string. If they are the same, move the first pointer S back one position to point to the next pointer, and repeat the above steps.

Finally, we can judge whether the string is a palindrome string according to the position of the pointer at the end of the loop, or whether the string is a palindrome string according to the end stage of the loop.

Β

### π code implementation

```# include "stdio.h"
# include "string.h"
# define SIZE 100

int main()
{
void Justhw(char *p1, char *p2);
char string[SIZE], *pStart, *pEnd;
int n;

printf("enter string(len<=%d):", SIZE);
scanf("%s", string);
n = strlen(string);

pStart= &string[0];
pEnd = &string[n-1];
Justhw(pStart, pEnd);

return 0;
}

void Justhw(char *p1, char *p2)
{
while (p1 < p2) {
if (*p1 != *p2) {
printf("This is not a palindrome string\n");// A
return;
}
p1++;
p2--;

}
// C

}```

### β¨ code annotation

```# include "stdio.h"
# include "string.h" / / use the strlen function to import its header file
# define SIZE 100

int main()
{
void Justhw(char *p1, char *p2);  // Initialization function
char string[SIZE], *pStart, *pEnd;  // Initialize pointer
int n;  // n indicates the length of the string

printf("enter string(len<=%d):", SIZE);
scanf("%s", string);  // Input string
n = strlen(string);   // strlen function gets the length of the string
//    if ((n&1) == 1) {
//        printf("this is not a palindrome string \n");
//        return 0;
//    }

pStart= &string[0];   // Make the first pointer pStart point to the first element of the string
pEnd = &string[n-1];  // Make the tail pointer pEnd point to the tail element of the string
Justhw(pStart, pEnd);   // Call function to determine whether palindrome

return 0;
}

void Justhw(char *p1, char *p2)
{
while (p1 < p2) {  // The cycle stop condition is: tail pointer = > first pointer,
// Because when the tail pointer > the first pointer indicates that the two pointers have traversed the string and moved one more step.
// The equal sign here is mainly the middle string when the palindrome string is an odd number of characters
if (*p1 != *p2) {
printf("This is not a palindrome string\n");// A
return;  // The return here does not return any value. The purpose of using return is
// In order to stop the program when it is judged that the string is not a palindrome string, the following statement is not executed
}
p1++;  // First pointer moves back
p2--;  // Tail pointer forward

}
// C
printf("This is a palindrome string\n"); // B

}```

If you want to judge whether the palindrome is based on the position of the pointer at the end of the loop, delete the statements at A and B and fill in C

```   if (p1-1 == p2 || p1 == p2)
printf("this is the palindrome string \n");
else
printf("this is not a palindrome string \n");```

If palindrome, the string length parity is the same

When the string length is an even number, after the loop ends, the two pointers move to meet and move one more step, and the two pointers are still adjacent, then P1-1 = P2 (equivalent to p1 = p2+1)

Β

When the string length is even, the two pointers just meet the middle character p1 = p2, and then move one more step after the cycle ends

Β

That's all for today. See you next time. π

ββββββββββββββββββββendββββββββββββββββββββββββββ

Tags: Algorithm pointer C

Posted by RClapham on Sun, 03 Jul 2022 03:56:17 +0930