1.统计字符串s在字符串str中出现的次数
#include#include #include int MySearch( char* str, char* s ) { char* p; int n =0; for( ; *str; ) if( ( p = strstr( str, s ) ) != ___1___ ) { n++; str=p+1; } else ___2___; return( ___3___ ); } main() { char str1[81], str2[21]; printf("nPlease enter str1 :"); gets(str1); printf("nplease enter str2 :"); gets(str2); printf( "nn"%s" are(is) appeared in str1 %d timesn", str2, MySearch(str1, str2)); }
2. 删除字符串 str 中所有 c 字符,并返回所删字符的个数。
#include#include int MyDelete( char* str, char c ) { int i, j=0, k=0; for( i = 0; str[i]; i++ ) if( str[i] != c ) { str[ j ]= str[i]; ___1___; } else k++; str[j]=___2___ ; return( ___3___ ); } main() { char string[81], x; printf("nPlease enter a string:n"); gets(string); printf("nPlease enter a character: "); scanf("%c", &x ); printf( "nnAfter deleting %d '%c'(s), the string becomes:n%sn",MyDelete(string, x), x, string); }
3.将未在字符串s中出现而在字符串t中出现的字符形成一个新的字符串放在u中,u中字符按字符串t中字符顺序排列
#include#include void fun (char *s, char *t, char *u) { int i, j, sl, tl, k, ul=0; sl = strlen(s); tl = strlen(t); for (i=0; i =sl) { for (k=0; k =ul) u[ul++] = ___2___ ; } } ___3___=' '; } main() { char s[100], t[100], u[100]; printf("nPlease enter string s:"); scanf("%s", s); printf("nPlease enter string t:"); scanf("%s", t); fun(s, t, u); printf("The result is: %sn", u); }
4.将在字符串S中下标为奇数位置上的字符,紧随其后重复出现一次,放在一个新串T中,T中字符按原字符串中字符的顺序排列.(注意0为偶数)
#include#include void fun (char *s, char *t) { int i, j, sl; sl = strlen(s); if(sl%2) sl-=2; ___1___ sl--; for (i=sl, j=___2___; i>=0; i-=2) { t[2*j] = s[i]; t[2*j +1] = ___3___ ; j++; } t[2*j]=' '; } main() { char s[100], t[100]; printf("nPlease enter string s:"); scanf("%s", s); fun(s, t); printf("The result is: %sn", t); }
5.给定程序中,函数fun的功能是:求出形参ss所指字符串数组中最长字符串的长度,将其余字符串右边用字符*补齐,使其与最长的字符串等长。ss所指字符串数组中共有M个字符串,且串长。 请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。 注意:源程序存放在考生文件夹下的BLANK1.C中。不得增行或删行,也不得更改程序的结构! 给定源程序
#include#include #define M 5 #define N 20 void fun(char (*ss)[N]) { int i, j, k=0, n, m, len; for(i=0; i n) { n=len; ___1___=i; } } for(i=0; i =0; j--) ss[i][m--]=ss[i][j]; for(j=0; j 6.
给定程序MODI1.C中函数fun()的功能是:将p所指字符串中每个单词的最后一个字母改成大写(这里的“单词”是指由空格隔开的字符串)。
#include#include #include void fun( char *p ) { int k = 0; for( ; *p; p++ ) if( k ) { if( p == ' ' ) { k = 0; * (p-1) = toupper( *( p - 1 ) ) } } else k = 1; } NONO( ) { char s1[81]; FILE *rf, *wf ; rf = fopen("g05.in", "r") ; wf = fopen("g05.out", "w") ; fgets(s1, 80, rf) ; fun(s1); fprintf( wf,"%s", s1 ); fclose(rf) ; fclose(wf) ; } main() { char chrstr[64]; int d ; printf( "nPlease enter an English sentence within 63 letters: "); gets(chrstr); d=strlen(chrstr) ; chrstr[d] = ' ' ; chrstr[d+1] = 0 ; printf("nnBefore changing:n %s", chrstr); fun(chrstr); printf("nAfter changing:n %s", chrstr); NONO(); } 7. 求出s所指字符串中最后一次出现t所指字符串的地址。
#include#include #include char * fun (char *s, char *t ) { char *p , *r, *a; a = Null; while ( *s ) { p = s; r = t; while ( *r ) if ( r == p ) { r++; p++; } else break; if ( *r == ' ' ) a = s; s++; } return a ; } main() { char s[100], t[100], *p; printf("nPlease enter string S :"); scanf("%s", s ); printf("nPlease enter substring t :"); scanf("%s", t ); p = fun( s, t ); if ( p ) printf("nThe result is : %sn", p); else printf("nNot be found !n" ); } 8.将 tt 所指的字符串中所有的小写字母都转换成对应的大写字母其他字符不变。
#include#include #include char* fun( char tt[] ) { int i; for( i = 0; tt[i]; i++ ) if(( 'a' <= tt[i] )||( tt[i] <= 'z' ) ) tt[i] += 32; return( tt ); } NONO( ) { char tt[81], ch; FILE *rf, *wf ; rf = fopen("g10.in", "r") ; wf = fopen("g10.out", "w") ; fgets(tt, 80, rf) ; fun(tt) ; fprintf( wf,"%s", tt ); fclose(rf) ; fclose(wf) ; } main( ) { int i; char tt[81]; printf( "nPlease enter a string: " ); gets( tt ); printf( "nThe result string is:n%s", fun( tt ) ); NONO(); } 9.将s所指字符串中位于奇数位置的字符或ASCII码为偶数的字符放入t所指数组中(规定第一个字符放在第0位中)。
例如,字符串s中的数据为:AABBCCDDEEFF,则字符串t中的数据为:ABBCDDEFF。#include#include #include #define N 80 void fun(char *s, char t[]) { int i, j=0; for(i=0; i


