C:回文:不同的strlen值 [英] C: Palindrome: Different strlen-values

查看:112
本文介绍了C:回文:不同的strlen值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个函数,该函数可以反转字符串并检查字符串是否是回文.当我用诸如"abba"之类的明显回文测试功能时,该功能表示它不是回文.另外,正向字符串和反向字符串的字符串长度也不同!

I'm making a function which reverses a string and checks if the string is a palindrome or not. When I test the function with a obvious palindrome like "abba", the function says it's not a palindrome. Also the forward string and the reversed string also differ in string length!

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

char forward [] = "abba"; //The string to be reversed

int size = (sizeof(forward)/sizeof(forward[0]) - 1);
int j = 0;
char reverse [10];

void reverser(char forward []) {

    printf("%s", "String forward: ");
    for (int i = 0; i < size; i++) { //Function for printing out the forward string.
        printf("%c", forward[i]);
    }

    printf("\n");

    printf("%s", "String reversed: ");

    for (int i = size, j = 0; i >= 0; --i, ++j) { //Function for reversing the string and printing it.
        reverse[j] = forward[i];
        printf("%c", reverse[j]);       
    }

    printf("\n");

    if (strcmp(forward, reverse) != 0) { //Using strcmp to check if the forward and reversed string are the same. 
        printf("Not a palindrome!\n");
    }
    else{
        printf("A palindrome!\n");
    }

    printf("Forward string length: %d\n",strlen(forward));
    printf("Reversed string length: %d\n",strlen(reverse));
} 

int main () {
    reverser(forward);      
}

输出:
向前的字符串:abba
字符串反转:abba
不是回文!
前进字符串长度:9
反向字符串长度:0

Output:
String forward: abba
String reversed: abba
Not a palindrome!
Forward string length: 9
Reversed string length: 0

推荐答案

在此循环中

for (int i = size, j = 0; i >= 0; --i, ++j) { //Function for reversing the string and printing it.
    reverse[j] = forward[i];
    printf("%c", reverse[j]);       
    }

forward [i]是变量i的初始值等于size

forward[i] is the terminating zero of the string when the initial value of variable i is equal to size

尝试以下循环实现

for ( int i = size, j = 0; i != 0; ++j) { //Function for reversing the string and printing it.
    reverse[j] = forward[--i];
    printf("%c", reverse[j]);       
    }

还要考虑到对于将存储由运算符sizeof或函数strlen返回的值的变量,最好使用类型size_t.

Also take into account that for variables that will store values returned by the operator sizeof or by the function strlen it is better to use type size_t.

所以在这些声明中

printf("Forward string length: %d\n",strlen(forward));
printf("Reversed string length: %d\n",strlen(reverse));

使用格式说明符%zu代替%d

例如

printf("Forward string length: %zu\n",strlen(forward));
printf("Reversed string length: %zu\n",strlen(reverse));

这里的一些回答者说,您必须对字符串reverse进行零终止.但是,它已经是零终止的,因为它是在任何函数外部声明的,因此具有静态存储持续时间,因此默认情况下它是零初始化的.

Some answerers here said that you have to zero-terminate the string reverse. However it is already zero-terminated because it is declared outside any function and as result has the static storage duration and as such it is zero-initialized by default.

这篇关于C:回文:不同的strlen值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆