4.30 删除字符串中的空格

来源:互联网 发布:数据透视表 实例教程 编辑:程序博客网 时间:2024/06/03 05:03
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char str[50];
char str_2[50];
//用数组实现
char delete_space(char a[50])
{
    char temp[50];
    int i = 0;
    int j = 0;
    while(a[i] != '\0')
    {
    //    printf("*\n");
        if(a[i] != ' ')
        {
            temp[j] = a[i];
            i++;
            j++;
        //   a++;
        //    temp++;
        }
        else if(a[i] == ' ')
        {
            i++;
        }
    }
    temp[j] = '\0';
    printf("%s\n",temp);
    return 0;
}

char delete_space_by_printor(char *b)
{
    char *p,*q;
    for(p=b;*p!='\0';p++)
    {
        q=p;
        if(*q==' ')
        {
            while(*q != '\0')
            {
                *q = *(q+1);
                q++;
            }
        }
    }
    printf("%s\n",b);
    return 0;
}

void main()
{
  //  char str[50];
//    fflush();
    printf("please input a string\n");
    gets(str);
    printf("after delete space:\n");
    delete_space(str);
    printf("please input a string 2:\n");
    gets(str_2);
    printf("after delete space_str_2:\n");
    delete_space_by_printor(str_2);

}
0 0
原创粉丝点击