首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

判断字符串回文怎么去除空格

2013-08-01 
判断字符串回文如何去除空格#include stdio.h#include string.h#include stdlib.h#include malloc.

判断字符串回文如何去除空格

#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;
    }

}

求大神指点。谢谢 回文
[解决办法]
可以参考我的博文(http://blog.csdn.net/turingo/article/details/8314263)。
[解决办法]
将tup(char *s)函数改成如下:

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);
}
}
}    

不明白的话,请回复!
[解决办法]
strsep 和 strtok两个库函数 会帮助你的

热点排行