第 1 頁:題目1 |
第 2 頁:題目2 |
第 3 頁:題目3 |
第 4 頁:題目4 |
第 5 頁:題目5 |
**題目3
函數(shù)ReadDat( )實(shí)現(xiàn)從文件IN.DAT中讀取一篇英文文章存入到字符串?dāng)?shù)組xx中;請編制函數(shù)StrOR( ),其函數(shù)的功能是:以行為單位依次把字符串中所有小寫字母o左邊的字符串內(nèi)容移到該串的右邊存放,然后把小寫字母o刪除,余下的字符串內(nèi)容移到已處理字符串的左邊存放,之后把已處理的字符串仍按行重新存入字符串?dāng)?shù)組xx中。最后main()函數(shù)調(diào)用函數(shù)WriteDat()把結(jié)果xx輸出到文件OUT5.DAT中。
例如:原文:n any field.Yu can create an index
you have the correct record.
結(jié)果:n any field. Yu can create an index
rd. yu have the crrect rec
原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個(gè)字符,含標(biāo)點(diǎn)符號和空格。
注意:部分源程序存放在文件prog1.c中。
請勿改動(dòng)主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
#include
#include
#include
char xx[50][80] ;
int maxline = 0 ; /* 文章的總行數(shù) */
int ReadDat(void) ;
void WriteDat(void) ;
void StrOR(void)
{int i,righto,j,s,k;
char tem[80];
for(i=0;i
for(j=strlen(xx[i])-1;j>=0;j--)
{ k=0;
memset(tem,0,80); /*初始化字符串?dāng)?shù)組tem*/
if(xx[i][j]=='o‘) /*如果當(dāng)前字符為'o',進(jìn)入以下語句*/
{righto=j; /*則將此字符中位置j的值賦給righto*/
for(s=righto+1;s
tem[k++]=xx[i][s]; /*從righto的下一跳開始將其后所有的字符都存入到tem中*/
for(s=0;s
if(xx[i][s]!='o‘) tem[k++]=xx[i][s]; /*將不是字符'o’的字符全存入到tem中*/
strcpy(xx[i],tem); /*將當(dāng)前已處理的字符重新存入當(dāng)前行xx*/
}
else continue;
}
}
void main()
{
clrscr() ;
if(ReadDat()) {
printf(“數(shù)據(jù)文件IN.DAT不能打開!\n\007”) ;
return ;
}
StrOR() ;
WriteDat() ;
}
int ReadDat(void)
{
FILE *fp ;
int i = 0 ;
char *p ;
if((fp = fopen(“IN.DAT”, “r”)) == NULL) return 1 ;
while(fgets(xx[i], 80, fp) != NULL) {
p = strchr(xx[i], '\n‘) ;
if(p) *p = 0 ;
i++ ;
}
maxline = i ;
fclose(fp) ;
return 0 ;
}
void WriteDat(void)
{
FILE *fp ;
int i ;
clrscr() ;
fp = fopen(“OUT5.DAT”, “w”) ;
for(i = 0 ; i < maxline ; i++) {
printf(“%s\n”, xx[i]) ;
fprintf(fp, “%s\n”, xx[i]) ;
}
fclose(fp) ;
}
解法二:
void StrOR(void)
{ int i;
char a[80],*p;
for(i=0;i
{ p=strchr(xx[i],'o’);
while(p)
{ memset(a,0,80);
memcpy(a,xx[i],p-xx[i]);
strcpy(xx[i],p+1);
strcat(xx[i],a);
p=strchr(xx[i],'o‘);
}
}
}
解法三:
void StrOR(void)
{ int i,j; char yy[80],*p;
for(i=0; i
for(j=0; j
if(xx[i][j]=='o’)
{ p=&xx[i][j+1];
strcpy(yy,p); /*將指針p所指向的字符串拷貝到字符串yy中去*/
strncat(yy,xx[i],j); /*將字符串xx[i]中前j個(gè)字符連接到y(tǒng)y中*/
strcpy(xx[i],yy); /*將字符串yy重新拷貝到字符串xx[i]中去*/
j=0; /* 開始下一次的掃描。*/
}
}
相關(guān)庫函數(shù)解釋:
char *strncat(char *dest, const char *src, size_t maxlen)
功能:將字符串src中前maxlen個(gè)字符連接到dest中
相關(guān)頭文件:string.h
char *strcpy(char *dest, const char *src)
功能:將字符串src拷貝到字符串dest中去
相關(guān)頭文件:string.h
北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內(nèi)蒙古 |