判断字符串回文如何去除空格
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <ctype.h>
void tup(char *s);
void resort(char *s);//翻转
int main(void)
{
char s1[100];
char s2[100]={0};
int i;
printf("Please input a strng:\n");
gets(s1);
tup(s1);
printf("%s",s1);
for(i=0;i<strlen(s1);i++)
s2[i]=s1[i];
printf("字符串s2:%s\n",s2);
resort(s2);
printf("逆序后的字符串s2:%s\n",s2);
if(strcmp(s1,s2)==0) //返回值为0,s1等于s2
printf("This string is Palindrom!\n");
else
printf("This string is not Palindrom!\n");
return 0;
}
void tup(char *s)
{ int i,ct;
for (i=0;i<strlen(s);i++)
if(!isspace(s[i])) //如果是空格,要保持i不变
s[i] = toupper(s[i]);
else
continue; //for循环里,continue会执行i++
}
void resort(char *s) //逆序输出
{ int i,j;
char temp;
for (i=0,j=strlen(s)-1;i<j;i++,j--)//对j的初始化是关键
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
void tup(char *s)
{
int i;
for (i = 0; i < strlen(s); )
{
if(!isspace(s[i])) //如果是空格,要保持i不变
{
s[i] = toupper(s[i]);
i++;
}
else
{
strncpy(&s[i], &s[i+1], strlen(s)-i);
}
}
}