c语言 编写程序,将字符串str中包含的数字字符提取出来,对它们进行从小到大排序,非数字字符忽略不计.

如题所述

#include<stdio.h>
main()
{
 char str[100];
 int s[50];
 int i=0,j=0,count=0,flag=0,t=0;
 puts("请输入字符串:");
 gets(str);
 do                 //提取整数
 {
  if('0'<=str[i]&&str[i]<='9')
  {
   flag=1;
   t=10*t+str[i]-'0';
  }
  else
  {
   if(flag==1)
   {
    count++;
    s[j++]=t;
   }
   flag=0;
   t=0;
  }
  i++;
 }while(str[i-1]!='\0');
 for(i=0;i<count;i++)   //排序
 {
  for(j=i;j<count;j++)
   if(s[i]>s[j])
   {
    t=s[i];
    s[i]=s[j];
    s[j]=t;
   }
 }
 for(i=0;i<count;i++)  //输出
 {
  printf("%d ",s[i]);
 }
 printf("\n");

}

温馨提示:答案为网友推荐,仅供参考
相似回答