免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1128 | 回复: 3
打印 上一主题 下一主题

有关字符串输出的问题,不知道我的程序错在哪儿? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-10-22 23:27 |只看该作者 |倒序浏览
我定义了一个字符串,Byte[]="CS][PS,CS,44][PS,AWE,664][AA,"
我希望输出的结果是:
[PS,CS,44]
[PS,AS,66]
就是遇到有括号的就把括号和括号里面的内容输出.
不知道我的程序错在哪儿?
另外如果我要把结尾不完整的部分[AA存入Rest[]里又该怎么写?

望大家指点一二.谢了!

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char Rest[]="";
char Byte[]="CS][PS,CS,44][PS,AWE,664][AA,";
char command[] = "";

void execute(char check[])
{
for(int i=0;i<=strlen(check);i++)
  {
        if(check[i]=='[')
             for(int t=0; check[i]==']';t++)
                 {
                   command[t]=check[i];
                   i++;
         
                 }
  }
  puts(command);
  int c;
  c=getchar();              
}


int main()
{
  execute(Byte);


}

论坛徽章:
0
2 [报告]
发表于 2006-10-23 05:10 |只看该作者
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char Rest[]="";
char Byte[]="CS][PS,CS,4444444][PS,AWE,664][AAA,";

char command[] = "";

void m_memset(void *dest, char b, int size)
{
        int i;
        char *d;
       
        d=(char *)dest;
       
        for (i=0;i<size;i++) {*d=b;d++;}
}

void execute(char check[])
{
        unsigned len = strlen(check);

        for(unsigned i=0;i<len; i++)
        {         
        if(check[i]=='[')            
                {
                        int t;
                       
                        for(t=0; check[i]!=']' && i<len;t++, i++)                 
                                command[t]=check[i];                  
                                         
                        if (check[i]==']')
                        {
                                command[t]=']';
                                puts(command);
                                m_memset(command, 0, strlen(command));  // reset command to zero
                        }

                        if (i==len)
                        {
                                strcpy(Rest, command);  // copy rest
                                puts(Rest);
                        }

                }       

  }
        
}


int main()
{
  execute(Byte);

  return 0;
}

论坛徽章:
0
3 [报告]
发表于 2006-10-23 13:20 |只看该作者
搞定了!!多谢!

论坛徽章:
0
4 [报告]
发表于 2006-10-23 13:38 |只看该作者
有一点不明白,  为什么不直接用memset(command,0,strlen(command))清0,而要写个 函数?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP