利用指针字符串去除空格和字母

来源:互联网 发布:人物绘画教学软件 编辑:程序博客网 时间:2024/05/16 05:50

以下是代码:

#include <stdio.h>
#include <string.h>
int main()
{
 
 char *str = (char *)malloc(sizeof(char)*100); //分配内存空间
 printf("input you want: /n");
 scanf("%[^/n]",str); //这句代码,不太理解
 fun(str);
 return 0;
}

int fun(char *tempstr)
{
 int i=0;
 int len=strlen(tempstr);
 char *str1 = tempstr;
 char *str2 = (char *)malloc(sizeof(char)*len+1);
 char *headstr2 = str2; //记住头,以备输出用
 for(i;i<len;i++)
  {
   if(*tempstr!=' '&& *tempstr>='0' && *tempstr<='9'){
    *str2 = *tempstr;
   str2++;}
   tempstr++;
  }
  *str2 = "/0";
  printf(headstr2);
  free(headstr2);//释放
  return 0;
}

 

 

原创粉丝点击