码迷,mamicode.com
首页 > 移动开发 > 详细

C语言:从p所指字符串中找出ASCII码最大的字符,将其放在第一个位置上,并将该字符前的原字符向后顺序移动。

时间:2019-03-18 13:25:05      阅读:902      评论:0      收藏:0      [点我收藏+]

标签:turn   ascii码   %s   bsp   ascii   please   位置   nbsp   code   

//fun函数:从p所指字符串中找出ASCII码最大的字符,将其放在第一个位置上,并将该字符前的原字符向后顺序移动。

 1 #include <stdio.h>
 2 void fun( char *p )
 3 {   char   max,*q;   int   i=0;
 4     max=p[i];
 5     while( p[i]!=0 )
 6     {   if( max<p[i] )
 7     {  max=p[i];
 8 /**********found**********/
 9     q = p + i;//先找到最大值,记录最大值的位置。
10     }
11         i++;
12     }
13 /**********found**********/
14     while(q>p )
15     {  *q=*(q-1);//进行顺序后移。
16        q--;
17     }
18     p[0]=max;
19 }
20 void main()
21 {  char   str[80];
22    printf("Enter a string:  "); gets(str);
23    printf("\nThe original string:      ");  puts(str);
24    fun(str);
25    printf("\nThe string after moving:  ");  puts(str); printf("\n\n");
26 }

//fun函数:根据一下公式求圆周率值,并作为函数返回值。Π/2=1+1/3+1/3*2/5+1/3*2/5*3/7+...

 1 #include <math.h>
 2 #include <stdio.h>
 3 double fun(double  eps)
 4 {  double  s,t;     int  n=1;
 5    s=0.0;
 6 /************found************/
 7    t=1+1/3;
 8    while( t>eps)
 9    {  s+=t;
10       t=t * n/(2*n+1);
11       n++;
12    }
13 /************found************/
14    return (s*2);
15 }
16 void main()
17 {  double  x;
18    printf("\nPlease enter a precision: "); scanf("%lf",&x);
19    printf("\neps=%lf, Pi=%lf\n\n",x,fun(x));
20 }

//规定输入的字符串中只包含字母和*号,fun函数:使字符串的前导*号不得多于n个,若多余n个,则删除多余的*号,若少于或等于n个,则不做处理,字符串尾部和中间的*号不删除。

 1 #include <stdio.h>
 2 void  fun( char *a, int  n )
 3 {
 4     char *b;
 5     b = a;
 6     int i = 0;
 7     while (!(A <= *b&&*b <= Z))
 8     {
 9         //printf("%c\n", *b);
10         b++;
11         i++;
12     }
13     if (i > n)
14     {
15         a = a + n;
16         while (*b != \0)
17         {
18             *a = *b;
19             a++;
20             b++;
21         }
22         *a = \0; 
23     }
24 }
25 
26 void main()
27 {  char  s[81];  int  n;void NONO ();
28    printf("Enter a string:\n");gets(s);
29    printf("Enter n :  ");scanf("%d",&n);
30    fun( s,n );
31    printf("The string after deleted:\n");puts(s);
32    NONO();
33 }
34 void NONO ()
35 {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
36   FILE *in, *out ;
37   int i, n ; char s[81] ;
38   in = fopen("in.dat","r") ;
39   out = fopen("out.dat","w") ;
40   for(i = 0 ; i < 10 ; i++) {
41     fscanf(in, "%s", s) ;
42     fscanf(in, "%d", &n) ;
43     fun(s,n) ;
44     fprintf(out, "%s\n", s) ;    
45   }
46   fclose(in) ;
47   fclose(out) ;
48 }

//使用数组完成:

    /*char b[81];
    int z = 0;
    while (*a != ‘\0‘)
    {
        b[z] = *a ;
        z++; a++;
    }
    b[z] = ‘\0‘;
    int i = 0;
    while (!(‘A‘ <= b[i]&&b[i] <= ‘Z‘))
    {
        i++;
    }
    if (i > n)
    {
        while (b[i] != ‘\0‘)
        {
            a = a + n;
            *a = b[i];
            a++;
            i++;
        }
        *a = ‘\0‘;
    }    */

 

C语言:从p所指字符串中找出ASCII码最大的字符,将其放在第一个位置上,并将该字符前的原字符向后顺序移动。

标签:turn   ascii码   %s   bsp   ascii   please   位置   nbsp   code   

原文地址:https://www.cnblogs.com/ming-4/p/10551482.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!