首页 > 试题广场 >

请仅编写一C语言函数char *maxword(char *

[问答题]

请仅编写一C语言函数char *maxword(char *s,char *t),该函数的功能是求出字符串s与字符串t的最长公共单词(这里,假设两个字符串均由英文字母和空格字符组成);若找到这样的公共单词,函数返回该单词,否则,函数返回NULL。

例如:若s=“This is C programming text”,t=“This is a text for C programming”,则函数返回“programming”。

要求:

(1) 函数中不得设置保存单词的存储空间;

(2) 给出函数之前请问文字简要叙述函数的基本思想。

#include <stdio.h>
#
include<string.h>
char *maxword(char *s,char *t){


    char res,*temp,chs,cht;
    int i,j,fd,maxl=0;
    while(*s!="\0"){
        while(*s==" ")
          s++;
        for(i=0;s[i]!=" "&&s[i]!="\0";i++)
            if(i>maxl){
                chs=s[i];
                s[i]="\0";
                temp=t;
                fd=0;
                while(*temp!="\0"&&!fd){
                    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){
                            maxl=i;
                            res=s;
                            fd=1;
                        }
                        temp=cht;
                    }
                    temp=&temp[j];
                }
                s[i]=chs;
            }
            s=&s[i];
        }
        
    if(maxl==0)
      return NULL;
      else{
          res[maxl+1]="\0";
          return res;
      }
    }
发表于 2020-03-22 16:49:10 回复(0)
/*==============================================================================================
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 # Auther:                Wenhao    1795902848@qq.com
 # Filename:            C souce code
 # Environment:            OS X Yosemite 10.10.5
 # Tool:                Vim & gcc
 # Date:                2018.12.16
 # Description:            找到两个字符串中的最长公共单词,思路很直接,定一动一,逐一比对判断每个单词
______________________________________________________________________________________________
=============================================================================================*/
#include <stdio.h>
char s[200], t[200];

char* maxword(char *s,char *t)
{
    while(*s == ' ') s++;
    while(*t == ' ') t++;

    int max_len = 0;
    char* max_start = NULL;
    char* max_end = NULL;

    char* s_start = s;
    char* s_end = s;
    int s_len = 0;

    char* t_start = t;
    char* t_end = t;
    int t_len = 0;

    while(*s_end != '\n')
    {
        for(s_len = 0; *s_end != ' ' && *s_end != '\n'; s_end++, s_len++);  //从s中找到了一个单词,长度为s_len
        t_start = t_end = t;
        while(*t_start != '\n')  //对于当前找到的s中的一个单词, 遍历t中的单词进行比对
        {
            for(t_len = 0; *t_end != ' ' && *t_end != '\n'; t_end++, t_len++);  //从t中找到了一个单词,长度为t_len
            if(s_len==t_len && *s_start==*t_start)  //长度相同,单词***同
            {
                int flag = 0;  //用来判断两单词是否相同
                char* tmp_pos = s_start;
                for(; tmp_pos!=s_end; tmp_pos++, t_start++)
                    if(*tmp_pos != *t_start)
                    {
                        flag = 1;  //当出现不匹配时跳出循环
                        break;
                    }
                if(!flag && max_len < s_len)  //如果这两个单词相同,并且相同长度更长
                {
                    max_len = s_len;
                    max_start = s_start;  //记录下这个更长的单词的位置
                }
            }
            for(t_start = t_end; *t_start == ' '; t_start++); //更新t_start的位置,到新的字母开头或者'\n'位置
            t_end = t_start;
        }
        for(s_start = s_end; *s_start == ' '; s_start++); //更新s_start的位置,进行下一轮循环
        s_end = s_start;

    }
    if(max_start)
    {
        for(max_end = max_start; *max_end != ' ' && *max_end != '\n'; max_end++);
        *max_end = '\0';
    }
    return max_start;
}

int main()
{
    //Input example:
    //eg:= "This is C programming text";
    //eg:= "This is a text for C programming";

    while(fgets(s, sizeof(s), stdin) && fgets(t, sizeof(t), stdin))
    {
        char* word = maxword(s, t);
        printf("%s\n", word);
    }

    return 0;
}

编辑于 2018-12-16 01:25:33 回复(0)


include <stdio.h>

char maxword(chars, chart) { int wordlen=0,maxlen = 0; char 
{ int wordlen=0,maxlen = 0; char pt = t, ps = s, res = s; while (s != '\0') { while (s == ' ')s++; ps = s; while (s != ' '&&s != '\0')//找到一个单词 s++; wordlen = s - ps;//获取单词长度 while (pt != '\0')//在t中找到s中当前单词相同的首字母 { while (pt == ' ')pt++; if (*ps == *pt) { //找到首字母 s = ps; while (*pt == *s&&*s != ' '&&*s!='\0')//找t中与当前单词相同的单词 { s++; pt++; } if (*s != ' '&&*s != '\0') //没有找到对应的单词,t中不是一个完整的单词也不算找到,s长了 continue; else if (*pt != ' '&&*pt != '\0') { while (*pt != ' ')pt++; continue;//跳转找t的下一个单词的首字母 } else if (wordlen > maxlen)//找到对应的单词了,与之前找到的单词对比长度,大于则记录,小于则过; { res = ps; maxlen = wordlen; pt = t; break; } else pt = t; break; } while (*pt != ' '&&*pt!='\0') pt++; } continue; } if (maxlen == 0) return 0; else { res[maxlen] = '\0'; return res; }

}

int main() { char s[] = "text This is C Programmingg"; char t[] = "This is a Programmin text for C Programming "; char *word = maxword(s, t); printf("%s", word); getchar(); return 0; }


编辑于 2018-12-15 19:53:34 回复(0)