| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 547 人关注过本帖
标题:字符串匹配出现了BUG,怎么看都不明白哪错了,各位帮我看看吧
只看楼主 加入收藏
chuanglan
Rank: 2
等 级:论坛游民
威 望:2
帖 子:91
专家分:29
注 册:2012-8-14
结帖率:84.62%
收藏
 问题点数:0 回复次数:1 
字符串匹配出现了BUG,怎么看都不明白哪错了,各位帮我看看吧
程序代码:
#include<stdio.h>
#include<string.h>
#define string_size 500
typedef struct
{
  char ch[string_size];
  int len;
}String;

void StrAsign(String *S,char chs[])
{
   char *q = S->ch;
/*   q = (char *)malloc(sizeof(string_size));*/
   strcpy(q,chs);
   S->len = strlen(chs);
}

void StrInsert(String *S,int pos,char chs[])
{
   int La,Lb,Lc;
   int RemainSize,i;
   char B[string_size];
   La = pos;
   Lb = S->len - pos;
   Lc = strlen(chs);
   if( La+Lb+Lc <= string_size )
     {
        for(i = 0; i <= Lb; i++)
           B[i] = S->ch[pos+i-1];
        for(i = 0; i < Lc; i++)
           S->ch[pos+i-1] = chs[i];
        for(i = 0;i <= Lb; i++)
           S->ch[i+pos+Lc-1] = B[i];
     }
   else if(Lb+Lc < string_size)
     {
        RemainSize = string_size-(La+Lc);
        for(i = 0; i < RemainSize; i++)
           B[i] = S->ch[pos+i];
        for(i = 0; i < Lc; i++)
           S->ch[pos+i] = chs[i];
        for(i = 0;i < RemainSize; i++)
           S->ch[i+pos+Lc] = B[i];
     }
   else
     {
        RemainSize = string_size - La;
        for(i = 0;i < RemainSize; i++)
          S->ch[pos+i] = chs[i];
     }
}


void Compare(String S,char comstr[],int *pos)   // 匹配函数,单词用错了,别踩我额,嘿嘿
{
    int i,j,start;
    int comlen;
    comlen = strlen(comstr);
    for(i = 0; i < S.len-comlen; i++)    //用的是比较差的算法,每次从主串的下一个字符开始匹配
      {
         start = i;
         for(j = 0; j < comlen; j++)       // 每次遇到不能匹配的字符则终止循环,问题好像就出在这里,但真不知道为什么....
           {
              if(S.ch[start] != comstr[j])
                 break;
              start++;
           }
        if(j >= comlen)
          {
            printf("It's the i value %d\n",i);
            *pos = i;
            printf("This is the pos's value %d\n",*pos);
             break;
          }
      }
}

void Delete(String *S,char delstr[])
{
   int delen = strlen(delstr);
   int i,pos;
   Compare(*S,delstr,&pos);
   for(i = 0;i < delen; i++)
     {
       S->ch[pos] = S->ch[pos+delen];
       pos ++;
     }
}

void main()
{
  String S;
  char chars[string_size];
  char T[string_size];
  char delstr[string_size];
  char comstr[string_size];
  int pos,pos1;

  puts("Pls input the string and inserted str and it's position");
  scanf("%s%s%d",chars,T,&pos);
  StrAsign(&S,chars);
  puts("Now we have gotten the string of S");
  puts(S.ch);
  StrInsert(&S,pos,T);
  printf("after the insert String became as the following:  %s\n",S.ch);

  puts("please input the string to compare:");
  scanf("%s",comstr);
  Compare(S,comstr,&pos1);
  printf("the postion of the comstring is %d\n",pos1);
/*
  puts("Please input the string to del");
  scanf("%s",delstr);
  Delete(&S,delstr);
  printf("The deleted string is:\n%s",S.ch); */
  getch();
}

搜索更多相关主题的帖子: 500 字符串 
2012-11-23 15:52
chuanglan
Rank: 2
等 级:论坛游民
威 望:2
帖 子:91
专家分:29
注 册:2012-8-14
收藏
得分:0 
找到了,是插入函数里面没给S->len改变值,因此S->ch的实际长度没有那么多额
,还有我的删除函数也有问题现附上修改好的代码,谢谢各位的关注额,
程序代码:
#include<stdio.h>
#include<string.h>
#define string_size 500
typedef struct
{
  char ch[string_size];
  int len;
}String;

void StrAsign(String *S,char chs[])
{
   char *q = S->ch;
   strcpy(q,chs);
   S->len = strlen(chs);
}

void StrInsert(String *S,int pos,char chs[])
{
   int La,Lb,Lc;
   int RemainSize,i;
   char B[string_size];
   La = pos;
   Lb = S->len - pos;
   Lc = strlen(chs);
   if( La+Lb+Lc <= string_size )
     {
        for(i = 0; i <= Lb; i++)
           B[i] = S->ch[pos+i-1];
        for(i = 0; i < Lc; i++)
           S->ch[pos+i-1] = chs[i];
        for(i = 0;i <= Lb; i++)
           S->ch[i+pos+Lc-1] = B[i];
     }
   else if(Lb+Lc < string_size)
     {
        RemainSize = string_size-(La+Lc);
        for(i = 0; i < RemainSize; i++)
           B[i] = S->ch[pos+i];
        for(i = 0; i < Lc; i++)
           S->ch[pos+i] = chs[i];
        for(i = 0;i < RemainSize; i++)
           S->ch[i+pos+Lc] = B[i];
     }
   else
     {
        RemainSize = string_size - La;
        for(i = 0;i < RemainSize; i++)
          S->ch[pos+i] = chs[i];
     }
   S->len =  S->len + Lc;
}


int StrMatch(String S,char comstr[],int *pos)
{
    int i,j,start;
    int comlen;
    comlen = strlen(comstr);
    for(i = 0; i <= S.len-comlen; i++)
      {
         start = i;
         for(j = 0; j < comlen; j++)
           {
              if(S.ch[start] != comstr[j])
                 break;
              start++;
           }
        if(j >= comlen)
          {
             *pos = i + 1;
             return 1;
          }
      }
    if(i > S.len-comlen)
      puts("We didn't find the string!");
      return 0;
}

int Delete(String *S,char delstr[])
{
   int delen = strlen(delstr);
   int i,pos;
   if(!StrMatch(*S,delstr,&pos))
     return 0;
   for(i = 0; pos < S->len; i++)
     {
       S->ch[pos-1] = S->ch[pos-1+delen];
       pos ++;
     }
}

void main()
{
  String S;
  char chars[string_size];
  char T[string_size];
  char delstr[string_size];
  char comstr[string_size];
  int pos;

  // 字符串初始化与插入
  puts("Pls input the string and inserted str and it's position");
  scanf("%s%s%d",chars,T,&pos);
  StrAsign(&S,chars);
  puts("Now we have gotten the string of S");
  puts(S.ch);
  StrInsert(&S,pos,T);
  printf("after the insert String became as the following:  %s\n",S.ch);

  // 字符串的匹配
 /* puts("please input the string to compare:");
  scanf("%s",comstr);
  if(StrMatch(S,comstr,&pos))
    printf("the postion of the comstring is %d to %d\n",pos,pos+strlen(comstr)-1);  */

   // 字符串的删除
  puts("Please input the string to del");
  scanf("%s",delstr);
  if(Delete(&S,delstr))
   printf("The deleted string is:\n%s",S.ch);
  getch();
}


2012-11-23 20:27
快速回复:字符串匹配出现了BUG,怎么看都不明白哪错了,各位帮我看看吧
数据加载中...
 
   



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

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