| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2307 人关注过本帖
标题:在输入一段字符串找出其中连续的数字,并把他们分别存在地址指针数组中
只看楼主 加入收藏
fu2751653
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:38
专家分:100
注 册:2011-4-11
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:6 
在输入一段字符串找出其中连续的数字,并把他们分别存在地址指针数组中
程序代码:
#include<iostream>
using namespace std;
int mat(char *str,char *s[])
{
  int i,j=0,len;

  len=strlen(str);

 
  for(i=0;i<len;i++)
  {
     if(str[i]>=48&&str[i]<=57)
     {
        
        strcat(s[j],"str[i]");
        if(str[i+1]<48||str[i+1]>57)
        {
            strcat(s[j],"\0");
   
            j++;
        }
     }

 
  }
  return j;

}
int main()
{
  char str[100],*s[100];
  int i,j;
  strcpy(str,"\0");
  strcat(str,"1");

 
  cout<<"please input a serial of char:";
  //cin>>str;
  gets(str);
  j=mat(str,s);
  for(i=0;i<j;i++)
  {
   cout<<s[i]<<endl;

  }
  return 0;
}
编译没问题,但调试到mat函数时,那个strcat(s[j],"str[i]");时就有问题了,请问哪位大神知道应该怎么改,指针数组
搜索更多相关主题的帖子: 连续 字符串 
2012-11-07 12:53
fu2751653
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:38
专家分:100
注 册:2011-4-11
收藏
得分:0 
难道没人指导
2012-11-08 19:38
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:2 
c++风格的哦

DO IT YOURSELF !
2012-11-08 19:40
C_戴忠意
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:2
帖 子:575
专家分:1349
注 册:2011-10-21
收藏
得分:2 
编译没问题,但调试到mat函数时,那个strcat(s[j],"str[i]");时就有问题了,请问哪位大神知道应该怎么改,指针数组





strcat(str1,str2);把str1和str2连接起来为什么加  " "  ?

















编程之路定要走完……
2012-11-08 21:26
C_戴忠意
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:2
帖 子:575
专家分:1349
注 册:2011-10-21
收藏
得分:6 
程序代码:
#include<iostream>
using namespace std;
int mat(char *str,char *s[])
{
  int i,j=0,len;

  len=strlen(str);

  for(i=0;i<len;i++)
  {
     if(str[i]>=48&&str[i]<=57)
     {
       
        strcat(s[j],"str[i]");//这个函数用错了,把str[i]外边的双引号去掉。
        if(str[i+1]<48||str[i+1]>57)
        {
            strcat(s[j],"\0");
  
            j++;
        }
     }

  }
  return j;

}
int main()
{
  char str[100],*s[100];
  int i,j;
  strcpy(str,"\0");
  strcat(str,"1");

  cout<<"please input a serial of char:";
  //cin>>str;
  gets(str);
  j=mat(str,s);
  for(i=0;i<j;i++)
  {
   cout<<s[i]<<endl;

  }
  return 0;
}


编程之路定要走完……
2012-11-08 21:27
fu2751653
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:38
专家分:100
注 册:2011-4-11
收藏
得分:0 
如果把“”去掉的话,就变成字符了,这个好像编译都过不了,因为strcat函数是把后一个字符串粘贴到前面一个去
2012-11-08 23:02
fu2751653
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:38
专家分:100
注 册:2011-4-11
收藏
得分:0 
程序代码:
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;

int main()
{
  char str[100];
  char* s[100];
  int i,j=0,len;
  char ch[2];

 

 // char **p;
  strcpy(str,"\0");
  for(int m=0;m<100;m++)
  {
   s[m]=(char*)malloc(5*sizeof(char));
   strcpy(s[m],"\0");

 
  }

 

 
  cout<<"please input a serial of char:";
  //cin>>str;
  gets(str);
  //j=mat(str,s);
   len=strlen(str);
  
  for(i=0;i<len;i++)
  {
     if(str[i]>=48&&str[i]<=57)
     { 
        //p=s+j;
        ch[0]=str[i];
        ch[1]='\0';
        strcat(s[j],ch);
        if(str[i+1]<48||str[i+1]>57)
        {
            strcat(s[j],"\0");
   
            j++;
        }
     }
  }
  for(i=0;i<j;i++)
  {
   cout<<s[i]<<endl;

  }
  return 0;
}
后来终于把问题找出来,现在把那个发上去 ,程序还没有优化,发现问题的关键是没有给指针数组里面元素赋予内存空间
2012-11-10 23:53
快速回复:在输入一段字符串找出其中连续的数字,并把他们分别存在地址指针数组中 ...
数据加载中...
 
   



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

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