C程序有道題解析看不懂
C程序有道題解析看不懂
編寫一個(gè)程序求出兩個(gè)字符串:
s[]=”Thisis C programming text”
t[]=”Thisis a text for C programming”
包含最長(zhǎng)的相同單詞(同一字母的大小寫視為不同的字符).
程序:
#include
#include
void maxword(char *s,char *t)
{
char*res,*temp,chs,cht;
inti,j,found,maxlen=0;
while(*s!='\0')
{
while(*s==' ') s++;
for(i=0;s[i]!=' ' && s[i]!='\0';i++);
if(i>maxlen)
{
chs=s[i];
s[i]='\0';
temp=t;
found=0;
while (*temp!='\0' && found)
{
while (*temp==' ') temp++;
for (j=0;temp[j]!=' ' && temp[j]!='\0';j++);
if (j==i)
{
cht=temp[j];
temp[j]='\0';
if (strcmp(s,temp)==0)
{
maxlen=i;
res=s;
found=1;
}
temp[j]=cht;
}
temp=&temp[j];
}
s[i]=chs;
}
s=&s[i];
}
if(maxlen==0)
printf("沒有相同的單詞.\n");
else
{
chs=res[maxlen];
res[maxlen]='\0';
printf("%s\n",res);
res[maxlen]=chs;
}
}
main()
{
staticchar s[]="This is C programming text";
staticchar t[]="This is a text for C programming";
maxword(s,t);
}
看不懂,關(guān)鍵步驟的每一步是干什么用的,設(shè)置的每個(gè)變量分別起什么作用.
編寫一個(gè)程序求出兩個(gè)字符串:
s[]=”Thisis C programming text”
t[]=”Thisis a text for C programming”
包含最長(zhǎng)的相同單詞(同一字母的大小寫視為不同的字符).
程序:
#include
#include
void maxword(char *s,char *t)
{
char*res,*temp,chs,cht;
inti,j,found,maxlen=0;
while(*s!='\0')
{
while(*s==' ') s++;
for(i=0;s[i]!=' ' && s[i]!='\0';i++);
if(i>maxlen)
{
chs=s[i];
s[i]='\0';
temp=t;
found=0;
while (*temp!='\0' && found)
{
while (*temp==' ') temp++;
for (j=0;temp[j]!=' ' && temp[j]!='\0';j++);
if (j==i)
{
cht=temp[j];
temp[j]='\0';
if (strcmp(s,temp)==0)
{
maxlen=i;
res=s;
found=1;
}
temp[j]=cht;
}
temp=&temp[j];
}
s[i]=chs;
}
s=&s[i];
}
if(maxlen==0)
printf("沒有相同的單詞.\n");
else
{
chs=res[maxlen];
res[maxlen]='\0';
printf("%s\n",res);
res[maxlen]=chs;
}
}
main()
{
staticchar s[]="This is C programming text";
staticchar t[]="This is a text for C programming";
maxword(s,t);
}
看不懂,關(guān)鍵步驟的每一步是干什么用的,設(shè)置的每個(gè)變量分別起什么作用.
其他人氣:622 ℃時(shí)間:2020-05-19 00:33:19
優(yōu)質(zhì)解答
/*將字符串s中的第一個(gè)單詞,與字符串t中的所有單詞比較,看單詞是否相等.然后s中的第二個(gè)單詞比較,這樣依次循環(huán)*/#include #include void maxword(char *s,char *t){char *res,*temp,chs,cht;int i,j,found,maxlen=0;...*res,*temp,chs,cht; ????????????????????for (j=0;temp[j]!=' ' && temp[j]!='\0';j++); ?????к??j?????????????λ???????????λ?????j-1,??????chs=temp[j]?? ???????jλ??????????????cht????*res???????????????temp=t??????temp??t?????в?????????for (j=0;temp[j]!=' ' && temp[j]!='\0';j++);??????temp[0]??temp[j-1]????????temp[j]='\0',???temp?????????????????????
我來回答
類似推薦
- 15 分鐘前清風(fēng)扶竹44 | 分類:| 瀏覽9次
- X+1>0 X
- 有道題目看不懂?
- 有道英語(yǔ)題看不懂
- 我看不懂,能不能幫我解答下,
- 草字頭+佳 是什么字
- empty what is full,fill what is empty! 永遠(yuǎn)不讓自己空虛,永遠(yuǎn)不讓自己自滿 給點(diǎn)點(diǎn)評(píng)
- 16S rRNA基因通用引物1492r/F27,1492和27分別是什么意思?編號(hào)么?
- 用短除法分解素因素:42 81 40
- 在長(zhǎng)1.6米,寬1.2米的長(zhǎng)方形三合板上,裁出半徑是20厘米的圓,最多可裁多少個(gè)?
- 線性代數(shù) 設(shè)A,B,C均為n階矩陣,I為n階單位矩陣,且ABC=I,則下列矩陣乘積一定等于I的是哪個(gè)?
- 試以下面的方程為例,敘述用分離變量法求解方程的步驟.
猜你喜歡
- 1每天堅(jiān)持朗讀對(duì)學(xué)外語(yǔ)有什么好處?
- 2You are yuji?急.
- 3思密達(dá)到底是什么意思
- 4計(jì)算(a的立方-b的立方)+ab(a-3b)-2(b的立方-a的平方b)
- 5求寫英語(yǔ)書面表達(dá)
- 6油酸的作用是什么
- 7請(qǐng)幫忙翻譯:Payment and and Charging
- 8近紅外區(qū)的吸收光譜代表什么含義?
- 9大正方形邊長(zhǎng)為15cm,小正方形邊長(zhǎng)為10cm.求陰影甲的面積比陰影乙的面積大多少平
- 10有甲乙丙三種文具,若購(gòu)甲2件,乙1件、丙3件共需23元,若夠甲1件、乙4件、丙5件共需36元,問夠甲一件,乙2件,丙3件共需多少元
- 11Jack has a dog and so have I.____dog and____had a fight
- 12英語(yǔ)翻譯