C程序有道题解析看不懂编写一个程序求出两个字符串:s[]=”Thisis C programming text”t[]=”Thisis a text for C programming”包含最长的相同单词(同一字母的大小写视为不同的字符).程序:#include #inc

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/04 21:56:15
C程序有道题解析看不懂编写一个程序求出两个字符串:s[]=”Thisis C programming text”t[]=”Thisis a text for C programming”包含最长的相同单词(同一字母的大小写视为不同的字符).程序:#include #inc

C程序有道题解析看不懂编写一个程序求出两个字符串:s[]=”Thisis C programming text”t[]=”Thisis a text for C programming”包含最长的相同单词(同一字母的大小写视为不同的字符).程序:#include #inc
C程序有道题解析看不懂
编写一个程序求出两个字符串:
s[]=”Thisis C programming text”
t[]=”Thisis a text for C programming”
包含最长的相同单词(同一字母的大小写视为不同的字符).
程序:
#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);
}
看不懂,关键步骤的每一步是干什么用的,设置的每个变量分别起什么作用.

C程序有道题解析看不懂编写一个程序求出两个字符串:s[]=”Thisis C programming text”t[]=”Thisis a text for C programming”包含最长的相同单词(同一字母的大小写视为不同的字符).程序:#include #inc
/*将字符串s中的第一个单词,与字符串t中的所有单词比较,看单词是否相等.然后s中的第二个单词比较,这样依次循环*/
#include
#include
void maxword(char *s,char *t)
{
char *res,*temp,chs,cht;
int i,j,found,maxlen=0;
while(*s!='\0')
{
while(*s==' ') s++; /*找出s字符串第一个不为空的字符位置*/
for(i=0;s[i]!=' ' && s[i]!='\0';i++);/*计算出单词长度*/
if(i>maxlen)
{
chs=s[i]; /*将s[[i]的值放入chs*/
s[i]='\0';
temp=t;
found=0;
while (*temp!='\0' && !found)
{
while (*temp==' ') temp++;/*找出temp(也就是t)字符串第一个不为空的字符位置*/
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; /*将第一个单词放入res*/
found=1;
}
temp[j]=cht; /*两个字符串不一样则将temp[j]原值放回*/
}
temp=&temp[j]; /*两个单词不一样长时将temp[j]的地址赋予temp[*/
}
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()
{
static char s[]="This is C programming text";
static char t[]="This is a text for C programming";
maxword(s,t);
}

C程序有道题解析看不懂编写一个程序求出两个字符串:s[]=”Thisis C programming text”t[]=”Thisis a text for C programming”包含最长的相同单词(同一字母的大小写视为不同的字符).程序:#include #inc C语言编写一个程序求出1+2+3……1000 谁知道用C语言怎么编写一个能求出所有的水仙花数的程序? 用matlab编写一个程序:求出200以内的质数, 输入一个 4×4的矩阵,编写程序,求出其中的最小值及其所在的行号和列号,同时求出对角线元素之和.用C程序 怎样编写一个C语言程序比较两个数的大小? 有一个4*3的矩阵求出每行和每列的最小值如何编写C程序 编写一个程序计算两个数的绝对值之和 用c语言编写一个求三角形面积的程序 用C语言编写一个程序:从键盘输入n(0 用C语言编写程序,输入一个正整数n(1 用c语言编写一个程序,输出下列雪花图案! 编写一个C程序 用于计算如下数学公式 编写一个C程序,用于求下列数学公式 abc+cba=1333,其中a、b、c均为一位数,编写一个程序求出a、b、c分别代表什么数字.还需要编写一个程序?麻烦了?很重要的 求一个循环结构C语言程序题解答方式,有一数列:2/1,3/2,5/3,8/5,.求出这个数列前20项和! 编写C语言程序,输入两个的正整数,求出它们的和、差、积、商. vc++建立一个类sum,输入5*5的二维数组,编写程序实现:求出两对角线上个元素的和,求出对角建立一个类sum,输入5*5的二维数组,编写程序实现:求出两对角线上个元素的和,求出对角线上行,列下标均