字符串赋值给字符数组后乱码
#include<ctype.h>#include<stdio.h>#include<stdbool.h>#defineMSG_LEN80boolis_palindrom...
#include <ctype.h>
#include <stdio.h>
#include <stdbool.h>
#define MSG_LEN 80
bool is_palindrome(const char *message);
void read_line(char str[]);
int main(void)
{
char msg_str[MSG_LEN];
printf("Enter a message: ");
read_line(msg_str);
if (is_palindrome(msg_str))
printf("Palindrome\n");
else
printf("Not a palindrome\n");
return 0;
}
bool is_palindrome(const char *message){
bool flag=true;
char temp[MSG_LEN];
int i=0,j;
for (; *message != '\0'; message++)
if (isalpha(*message)){
temp[i]=tolower(*message);/*这里出现问题,temp输出乱码*/
i++;
}
for (j=0; j <= i-1 ; j++)
if (temp[j]!=temp[i-j]){
flag=false;
break;}
return flag;
}
void read_line(char str[])
{
int ch, i = 0;
while ((ch = getchar()) != '\n')
str[i++] = ch;
str[i] = '\0';
return;
} 展开
#include <stdio.h>
#include <stdbool.h>
#define MSG_LEN 80
bool is_palindrome(const char *message);
void read_line(char str[]);
int main(void)
{
char msg_str[MSG_LEN];
printf("Enter a message: ");
read_line(msg_str);
if (is_palindrome(msg_str))
printf("Palindrome\n");
else
printf("Not a palindrome\n");
return 0;
}
bool is_palindrome(const char *message){
bool flag=true;
char temp[MSG_LEN];
int i=0,j;
for (; *message != '\0'; message++)
if (isalpha(*message)){
temp[i]=tolower(*message);/*这里出现问题,temp输出乱码*/
i++;
}
for (j=0; j <= i-1 ; j++)
if (temp[j]!=temp[i-j]){
flag=false;
break;}
return flag;
}
void read_line(char str[])
{
int ch, i = 0;
while ((ch = getchar()) != '\n')
str[i++] = ch;
str[i] = '\0';
return;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询