简体   繁体   English

字符指针与C中的回文检查器

[英]Character Pointers with Palindrome Checker in C

This is code I wrote that checks if a string is a palindrome or not. 这是我写的代码,用于检查字符串是否是回文。 I need to revise this code so that it uses character pointers in it. 我需要修改这段代码,以便它在其中使用字符指针。 Could someone give me some suggestions/tips...or show me how to do that? 有人可以给我一些建议/提示......或者告诉我该怎么做? Thanks 谢谢

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(){
    char string1[20];
    int i, length;
    int flag = 0;
    printf("Enter a string: ");
    scanf("%s", string1);
    length = strlen(string1);
    for(i=0;i < length ;i++){
        if(toupper(string1[i]) != toupper(string1[length-i-1])){
            flag = 1;
            break;
        }
    }
    if (flag) 
        printf("%s is not a palindrome \n\n", string1); 
    else 
        printf("%s is a palindrome \n", string1);

    return 0;
}

In your code you use string1[i] to access the current element from the beginning of the string, and string1[length-i-1] to access the current element from the end of the string. 在您的代码中,您使用string1[i]从字符串的开头访问当前元素,并使用string1[length-i-1]从字符串的末尾访问当前元素。 You could create two pointers, pb and pe , and then move them toward each other. 你可以创建两个指针, pbpe ,然后将它们朝向彼此移动。

To define pointers, use this: 要定义指针,请使用:

char *pb = &string1[0]; // Or just string1, compiler will convert it to pointer
char *pe = &string1[length-1];

To advance the pointers toward each other, use pb++ and pe-- . 为了推进彼此的指针,使用pb++pe-- To see if the pointers have not crossed each other , check that pb < pe . 要查看指针是否相互交叉,请检查pb < pe Currently, your program checks the string twice; 目前,您的程序检查字符串两次; there's no need to do that - you can stop as soon as pe becomes less than or equal to the pb . 没有必要这样做 - 一旦pe变得小于或等于pb ,你就可以停止。 To access the character pointed to by the current pointer, use 要访问当前指针指向的字符,请使用

toupper(*pb) != toupper(*pe)

You can combine the check with advancing the pointers, like this: 您可以将检查与推进指针相结合,如下所示:

toupper(*pb++) != toupper(*pe--)

Note: it is not safe to use %s , because when users enter more characters than fits in your string1 buffer overrun results. 注意:使用%s是不安全%s ,因为当用户在string1缓冲区溢出结果中输入的字符多于拟合时。 You should specify the length of the buffer, like this: 您应该指定缓冲区的长度,如下所示:

scanf("%19s", string1); // Leave one char for null terminator

I'm not sure I completely understand the question, but I think this answers it. 我不确定我是否完全理解这个问题,但我认为这可以解决这个问题。 You actually are using character pointers. 你实际上是在使用字符指针。 char string1[20] is the same as char *string1. char string1 [20]与char * string1相同。 The difference is that you've basically assigned the pointer to a block of memory. 不同之处在于您基本上已将指针指向一块内存。 You could access the string in this way. 您可以通过这种方式访问​​字符串。

char string[20] = "foo";

printf("%c\n", string[0]);  // will print 'f'
printf("%c\n", *string); // will also print 'f'

printf("%c\n", string[1]); // will print the first 'o'
printf("%c\n", *(string + 1)); // will also print the first 'o'

with char * it goes like this 与char *它是这样的

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
    char string1[20];
    int i, length;
    int flag = 0;
    printf("Enter a string: ");
    scanf("%s", string1);
    length = strlen(string1);

    char *start=string1;
    char *end=&string1[length-1];

    //only check upto half


    for(i=0;i <= (length-1)/2 ;i++)
    {
        if(toupper(*(start+i)) != toupper(*(end-i)))
        {
            flag = 1;
            break;
        }
    }
    if (flag) 
        printf("%s is not a palindrome \n\n", string1); 
    else 
        printf("%s is a palindrome \n", string1);

    return 0;
}

cant we just copy the original string to another array, and then use strrev() to reverse the copied string and then finally compare the original string with the reversed string? 我们只是将原始字符串复制到另一个数组,然后使用strrev()来反转复制的字符串,然后最终将原始字符串与反向字符串进行比较?

Like this 像这样

1.get new string 
2.copy string to new array 
3.reverse the copied string using strrev 
4.use strcmp to check if both are same or not?

this seemed easier (i am a beginner so please correct me if i am wrong) 这似乎更容易(我是一个初学者所以如果我错了请纠正我)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM