C语⾔:将s所指字符串中下标为偶数同时ASCII值为奇数的字符删去,-将a所指字符串中的字。。。
//函数fun功能:将s所指字符串中下标为偶数同时ASCII值为奇数的字符删去,s所指串中剩余的字符形成的新串放在t所指的数组中。
1 #include <stdio.h>
2 #include <string.h>
3
4void fun(char *s, char t[])
5 {
6int i=0,j=0;
7while (s[i] != '\0')
8 {
9if (i % 2 == 0)
10 {
11if ((int)(s[i]) % 2 == 1)//判断ASCII值,使⽤(int)强制转换类型。
12 {
13//printf("%d", (int)s[i]);//调试语句
14 i++;
15 }
16else
17 {
18 t[j] = s[i];
19//printf("%c", *t);
20 i++; j++;
21 }
22 }
23else
24 {
25 t[j] = s[i];
26//printf("%c", *t);
27 i++; j++;
28 }
29 }
30 t[j] = '\0';//切记,切记,在赋值⼀个新数组的时候要记得添加结束符。
31 }
32
33void main()
34 {
35char s[100], t[100];void NONO ();
36 printf("\nPlease enter string S:"); scanf("%s", s);
37 fun(s, t);
38 printf("\nThe result is: %s\n", t);
39 NONO();
40 }
41
42void NONO ()
43 {/* 本函数⽤于打开⽂件,输⼊数据,调⽤函数,输出数据,关闭⽂件。 */
44char s[100], t[100] ;
45 FILE *rf, *wf ;
46int i ;
47
48 rf = fopen("in.dat","r") ;
49 wf = fopen("out.dat","w") ;
50for(i = 0 ; i < 10 ; i++) {
51 fscanf(rf, "%s", s) ;
52 fun(s, t) ;
53 fprintf(wf, "%s\n", t) ;
54 }
55 fclose(rf) ;
56 fclose(wf) ;
57 }
//函数fun功能:⾸先把b所指字符串中的字符按逆序存放,然后将a所指字符串中的字符和b所指字符串中的字符的顺序交叉,按排序合并到c 所指的数组,过长的剩余字符连接在数组的尾部。
1 #include <stdio.h>
2 #include <string.h>
3
4void fun( char *a, char *b, char *c )
5 {
6int i , j; char ch;
c 字符串转数组7 i = 0; j = strlen(b)-1;//指向尾部
8/************found************/
9while ( i < j )//⾸尾对应交换顺序。
10 { ch = b[i]; b[i] = b[j]; b[j] = ch;
11 i++; j--;
12 }
13while ( *a || *b ) {//⼀个⼀个进⾏放置
14/************found************/
15if ( *a )
16 { *c = *a; c++; a++; }
17if ( *b )
18 { *c = *b; c++; b++; }
19 }
20 *c = 0;//结束符
21 }
22
23void main()
24 {
25char s1[100],s2[100],t[200];
26 printf("\nEnter s1 string : ");scanf("%s",s1);
27 printf("\nEnter s2 string : ");scanf("%s",s2);
28 fun( s1, s2, t );
29 printf("\nThe result is : %s\n", t );
30 }
//函数fun功能:将形参s所指字符串中的所有数字字符顺序前移,其他字符顺序后移,处理后新字符串的⾸地址作为函数的返回值。
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <ctype.h>
5char *fun(char *s)
6 { int i, j, k, n; char *p, *t;
7 n=strlen(s)+1;//求出长度
8 t=(char*)malloc(n*sizeof(char));//申请内存
9 p=(char*)malloc(n*sizeof(char));
10 j=0; k=0;
11for(i=0; i<n; i++)
12 { if(isdigit(s[i])) {//函数判断是否为数字字符。
13/**********found**********/
14 p[j]=s[i]; j++;}
15else
16 { t[k]=s[i]; k++; }
17 }
18/**********found**********/
19for(i=0; i<k; i++) p[j+i]= t[i];//添加在p数组后⾯
20 p[j+k]=0;//结束符
21/**********found**********/
22return p;
23 }
24void main()
25 { char s[80];
26 printf("Please input: "); scanf("%s",s);
27 printf("\nThe result is: %s\n",fun(s));
28 }
//第⼀题标准答案。
1 {
2int i,j=0;
3for(i=0;i<strlen(s);i++)
4if(!((i%2)==0&&(s[i]%2)))
5 t[j++]=s[i];
6 t[j]=0;
7 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论