C語言:將字符串中的前導*號全部移到字符串的尾部。


//規定輸入的字符串中只包含字母和*號,fun函數:將字符串中的前導*號全部移到字符串的尾部。

 1 #include <stdio.h>
 2 void  fun( char *a )
 3 {
 4     char b[81];
 5     char *c,*d;
 6     c = a;
 7     int i = 0;
 8     while (*c == '*')
 9     {
10         c++;
11     }
12     d = c;
13     while (*c != '\0')
14     {
15         b[i] = *c;
16         i++;
17         c++;
18     }
19     int e = 0;
20     while (a < d)
21     {
22         b[i] = *a;
23         i++;
24         a++;
25         e++;
26     }
27     a = a - e;
28     for (int j = 0; j < i; j++)
29     {
30         *a = b[j];
31         a++;
32     }
33     *a = '\0';
34 }
35 
36 void main()
37 {  char  s[81];  int  n=0; void NONO ();
38    printf("Enter a string:\n");gets(s);
39    fun( s );
40    printf("The string after moveing:\n");puts(s);
41    NONO();
42 }
43 void NONO ()
44 {/* 本函數用於打開文件,輸入數據,調用函數,輸出數據,關閉文件。 */
45   FILE *in, *out ;
46   int i ; char s[81] ;
47   in = fopen("in.dat","r") ;
48   out = fopen("out.dat","w") ;
49   for(i = 0 ; i < 10 ; i++) {
50     fscanf(in, "%s", s) ;
51     fun(s) ;
52     fprintf(out, "%s\n", s) ;    
53   }
54   fclose(in) ;
55   fclose(out) ;
56 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM