| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 632 人关注过本帖
标题:程序没有输出预期的结果!
只看楼主 加入收藏
xinbuzai
Rank: 1
等 级:新手上路
帖 子:29
专家分:8
注 册:2010-7-2
结帖率:60%
收藏
已结贴  问题点数:20 回复次数:6 
程序没有输出预期的结果!
#include<stdio.h>
#include<string.h>
  char  maxword(char *s,char *t)
  {
      char *res,*temp,chs,cht;
      int i,j,found,maxlen=0;
      while(*s!='\0')
      {
         while(*s=='.')s++; //这里的while循环语句只有s++
         for(i=0;s[i]!='.'&&s[i]!='\0';i++);//这里的FOR里面为空语句
         if(i>maxlen)
            {
               chs=s[i];s[i]='\0';temp=t;found=0;
                while(*temp!='\0'&&!found)
                {
                   while(*temp=='.')temp++;
                   for(j=0;temp[i]!='.'&&temp[j]!='\0';j++);//这里的FOR里面为空语句
                   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("There is no same word.\n");
           else
             {
               chs=res[maxlen];res[maxlen]='\0';
               printf("%s\n",res);res[maxlen]=chs;
              }
}
  main()
{    char s[]="This is C programming test",t[]="This is a test for C programming";
      maxword(s,t);
      getch();
}
本段程序是在两个相的的字符串中找相同的字符串,偶写的没有语法问题,可以编译,但输出的结果是There is no same word。 不知道出了什么问题,那位可以帮偶看一下!
另外,这个temp[i]!='.'&&temp[j]!='\0'表达式里面的是什么意思?无论我把单引号里面的点去掉,还是不去掉,在语法上好像是没有错误的,能编译!

[ 本帖最后由 xinbuzai 于 2010-7-4 16:53 编辑 ]
搜索更多相关主题的帖子: 结果 输出 
2010-07-04 16:23
liuqianxue
Rank: 2
等 级:论坛游民
帖 子:21
专家分:20
注 册:2010-5-7
收藏
得分:6 

"


[ 本帖最后由 liuqianxue 于 2010-7-4 17:45 编辑 ]
2010-07-04 17:10
xinbuzai
Rank: 1
等 级:新手上路
帖 子:29
专家分:8
注 册:2010-7-2
收藏
得分:0 
以下是引用liuqianxue在2010-7-4 17:10:33的发言:

呵呵,以为你在判断指针S是否为空的时候错了
"while(*s!='\0')"应该改为"while(*s!=='\0'),我就看到这了
是你太粗心了~~~
"
有!==这种表达符号吗?
2010-07-04 17:16
liuqianxue
Rank: 2
等 级:论坛游民
帖 子:21
专家分:20
注 册:2010-5-7
收藏
得分:0 
呵呵,想错了,不要意思。
你能说说你的思路是怎样的吗,大家讨论讨论
2010-07-04 18:03
erfen
Rank: 2
等 级:论坛游民
帖 子:9
专家分:15
注 册:2010-5-30
收藏
得分:6 
temp[i]!='.'&&temp[j]!='\0'  这就是判断字串是否结束 - -  
感觉这程序挺乱的   
  学的不好看不懂
2010-07-04 23:00
l199256tt
Rank: 1
等 级:新手上路
帖 子:1
专家分:7
注 册:2010-7-5
收藏
得分:6 
问题在于两个FOR语句,你的两个FOR语句的下标都到了最后面,那么你的maxlen变量根本没赋值 当然编译不出来啦!
2010-07-05 18:57
xinbuzai
Rank: 1
等 级:新手上路
帖 子:29
专家分:8
注 册:2010-7-2
收藏
得分:0 
以下是改好的程序!

#include<stdio.h>
#include<string.h>
  char  maxword(char *s,char *t)
  {
      char *res,*temp,chs,cht;
      int i,j,found,maxlen=0;
      while(*s!='\0')
      {
         while(*s==' ')s++; //如果是空格字符则跳过,即跳过字符串前面的空格
         for(i=0;s[i]!=' '&&s[i]!='\0';i++);//判断s[i]字符是否为空格和字符串结束,如果是,则终止循环
         if(i>maxlen)
            {
               chs=s[i];s[i]='\0';temp=t;found=0;
                while(*temp!='\0'&&!found)
                {
                   while(*temp==' ')temp++;//如果字符串t前面字符是空格字符则跳过,即跳过字符串前面的空格

                   for(j=0;temp[j]!=' '&&temp[j]!='\0';j++);//判断temp[j]字符是否为空格和字符串结束,如果是,则终止循环
                   if(j==i)//看字符串s,t里的第一个字符串是否一样长
                       {
                          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("There is no same word.\n");
           else
             {
               chs=res[maxlen];res[maxlen]='\0';
               printf("%s\n",res);res[maxlen]=chs;
              }
}
  main()
{    char s[]="This is C programming test",t[]="This is a test for C programming";
      maxword(s,t);
      getch();
}


[ 本帖最后由 xinbuzai 于 2010-7-11 19:40 编辑 ]
2010-07-11 19:25
快速回复:程序没有输出预期的结果!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015732 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved