鱼C论坛

 找回密码
 立即注册
查看: 3407|回复: 8

c和指针问题大神帮一把啊

[复制链接]
发表于 2012-8-3 17:40:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
/*题目:从一个字符串里面删除 一个字符串、
当我输入abcde和bcd时(从abcde中删除bcd),就出错了*/

# include <stdio.h>

int del( char *str,                char  *sub )
{
        char *p = str,        *q = sub, *s;

        int i = 0;

        while( p )
        {
                while( *p++ == *q++ )
                {
                        i++;    //i计算删除的个数

                        if( *q == 0 )  //如果q已经结束
                        {
                                s = p;                //s指向p当前的位置
                                p = p - i;        //p退到需要删除的第一个字符的位置

                                while( *s )  //当s没结束时
                                {
                                        * p++ = * s++;   //把后面的依次复制到p当前的位置
                                }

                                *p = 0; //结束字符串
       
                                return 1;
                        }
                }

                q = sub;  //如果不等、就使q归为、p指向下一个元素
                p++;
        }

                return 0;
       
}

void main()
{
        char a[10], b[10];
       
        printf("Enter the a and b :\n");

        gets( a );
        gets( b );

        if( del( a, b ) )
        {
                printf("%s\n", a);
        }
        else
                printf("NULL!\n");



}
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-8-3 17:47:47 From FishC Mobile | 显示全部楼层
while那里有个错误
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-8-3 17:48:20 From FishC Mobile | 显示全部楼层
while(*s)
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-8-3 17:56:37 From FishC Mobile | 显示全部楼层
郁闷,看错了
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-8-3 18:04:07 From FishC Mobile | 显示全部楼层
你定义的是字符型a,b但你却用它来接收字符串
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-8-4 00:02:06 | 显示全部楼层
期待大神指导
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-8-4 10:18:35 | 显示全部楼层
while( *p++ == *q++ ) 你判断的时候p就+了一下,false的时候p又加了一下,所以。。。。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-8-4 12:30:28 | 显示全部楼层
本帖最后由 1799697736 于 2012-8-4 12:32 编辑
  1. //////////你这个代码被减的字符串必须包含用来减的字符串,否则也会出错
  2. # include <stdio.h>

  3. int del( char *str, char  *sub )
  4. {
  5.         char *p = str, *q = sub, *s;

  6.         int i = 0;

  7.         while( p )
  8.         {
  9.                 while( *p == *q )  //没多大改变,你不能在判断的时候用++,不然会很乱
  10.                 {
  11.                         p++;
  12.                         q++;

  13.                         i++;    //i计算删除的个数

  14.                         if( *q == 0 )  //如果q已经结束
  15.                         {
  16.                                 s = p;                //s指向p当前的位置
  17.                                 p = p - i;        //p退到需要删除的第一个字符的位置

  18.                                 while( *s )  //当s没结束时
  19.                                 {
  20.                                         * p++ = * s++;   //把后面的依次复制到p当前的位置
  21.                                 }

  22.                                 *p = 0; //结束字符串

  23.                                 return 1;
  24.                         }
  25.                 }

  26.                 q = sub;  //如果不等、就使q归为、p指向下一个元素
  27.                 p++;
  28.         }

  29.                 return 0;

  30. }

  31. void main()
  32. {
  33.         char a[10], b[10];

  34.         printf("Enter the a and b :\n");

  35.         gets( a );
  36.         gets( b );

  37.         if( del( a, b ) )
  38.         {
  39.                 printf("%s\n", a);
  40.         }
  41.         else
  42.                 printf("NULL!\n");
  43. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-8-4 20:01:08 | 显示全部楼层
第十二行到十五行
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-28 04:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表