C语言:将ss所指字符串中所有下标为奇数位上的字母转换成大写,若不是字母,则不转换。-删除指针p所指字符串中的所有空白字符(包括制表符,回车符,换行符)-在带头结点的单向链表中,查找数据域中值为ch的结点,找到后通过函数值返回该结点在链表中所处的顺序号,
//将ss所指字符串中所有下标为奇数位上的字母转换成大写,若不是字母,则不转换。
1 #include <stdio.h> 2 #include <string.h> 3 void fun ( char *ss ) 4 { 5 while(*ss) 6 { 7 ss++; 8 if (*ss >= 'a'&&*ss <= 'z') 9 { 10 *ss -= 32;//转化为小写 11 } 12 ss++; 13 } 14 } 15 16 void main( ) 17 { char tt[81] ; 18 void NONO ( ); 19 printf( "\nPlease enter an string within 80 characters:\n" ); gets( tt ); 20 printf( "\n\nAfter changing, the string\n \"%s\"", tt ); 21 fun( tt ); 22 printf( "\nbecomes\n \"%s\"\n", tt ); 23 NONO ( ); 24 } 25 26 void NONO ( ) 27 {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ 28 FILE *fp, *wf ; 29 char tt[81] ; 30 int i ; 31 32 fp = fopen("in.dat","r") ; 33 wf = fopen("out.dat","w") ; 34 for(i = 0 ; i < 10 ; i++) { 35 fscanf(fp, "%s", tt) ; 36 fun( tt ) ; 37 fprintf(wf, "%s\n", tt) ; 38 } 39 fclose(fp) ; 40 fclose(wf) ; 41 }
//利用数组解决。
1 void fun ( char *ss ) 2 { 3 for (int i = 1; ss[i] != '\0'; i++,i++) 4 { 5 if (ss[i] >= 'a'&&ss[i] <= 'z') 6 { 7 ss[i] = ss[i] - 32; 8 } 9 } 10 }
//删除指针p所指字符串中的所有空白字符(包括制表符,回车符,换行符)输入字符时用‘#’结束输入。
1 #include <string.h> 2 #include <stdio.h> 3 #include <ctype.h> 4 void fun ( char *p) 5 { int i,t; char c[80]; 6 /************found************/ 7 for (i = 0,t = 0; p[i] ; i++) 8 if(!isspace(*(p+i))) c[t++]=p[i]; 9 /************found************/ 10 c[t]='\0';//把处理后的新字符串赋值给c数组 11 strcpy(p,c);//把c赋值给p 12 } 13 14 void main( ) 15 { char c,s[80]; 16 int i=0; 17 printf("Input a string:"); 18 c=getchar(); 19 while(c!='#') 20 { s[i]=c;i++;c=getchar(); } 21 s[i]='\0'; 22 fun(s); 23 puts(s); 24 }
//在带头结点的单向链表中,查找数据域中值为ch的结点,找到后通过函数值返回该结点在链表中所处的顺序号,若不存在值为ch的结点,则返回0。
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define N 8 4 typedef struct list 5 { int data; 6 struct list *next; 7 } SLIST; 8 SLIST *creatlist(char *); 9 void outlist(SLIST *); 10 int fun( SLIST *h, char ch) 11 { SLIST *p; int n=0; 12 p=h->next; 13 /**********found**********/ 14 while(p!=NULL) 15 { n++;//记录链表位置 16 /**********found**********/ 17 if (p->data==ch) return n; 18 else p=p->next; 19 } 20 return 0; 21 } 22 void main() 23 { SLIST *head; int k; char ch; 24 char a[N]={'m','p','g','a','w','x','r','d'}; 25 head=creatlist(a); 26 outlist(head); 27 printf("Enter a letter:"); 28 scanf("%c",&ch); 29 /**********found**********/ 30 k=fun(head,ch); 31 if (k==0) printf("\nNot found!\n"); 32 else printf("The sequence number is : %d\n",k); 33 } 34 SLIST *creatlist(char *a) 35 { SLIST *h,*p,*q; int i; 36 h=p=(SLIST *)malloc(sizeof(SLIST)); 37 for(i=0; i<N; i++) 38 { q=(SLIST *)malloc(sizeof(SLIST)); 39 q->data=a[i]; p->next=q; p=q; 40 } 41 p->next=0; 42 return h; 43 } 44 void outlist(SLIST *h) 45 { SLIST *p; 46 p=h->next; 47 if (p==NULL) printf("\nThe list is NULL!\n"); 48 else 49 { printf("\nHead"); 50 do 51 { printf("->%c",p->data); p=p->next; } 52 while(p!=NULL); 53 printf("->End\n"); 54 } 55 }