首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

C语言,短信号码的提取。该如何解决

2012-12-31 
C语言,短信号码的提取。Input第一行有个整数n(1≤n≤1000)表示测试用例的个数。其后的每一行中有一条短信,每一

C语言,短信号码的提取。
Input
第一行有个整数n(1≤n≤1000)表示测试用例的个数。其后的每一行中有一条短信,每一条短信中只包含字母、数字、空格、标点符号,没有回车换行符,短信的长度不超过400个英文字符。
Output
将每条短信中的电话号码提取出来。每个号码占一行。如果该短信中没有电话号码,请输出“no phone numbers!” 每个测试用例的输出之间用一个空行隔开。 
Sample Input
2
Mr Zhang's home phone is 073112345678, and his office phone is 87654321, his mobile phone is 13812345678
Sorry, I don't have his any phone numbers!
Sample Output
073112345678
87654321
13812345678

no phone numbers!

下面是我写的代码,但是提示答案错误,求好心人给我解决一下,谢谢了。
#include<stdio.h>
#include<string.h>
int main() 
{
int n,i,r,t,j;
char s[800];
scanf("%d",&n);
getchar();
for(i=0;i<n;i++)
{
r=1;
gets(s);
t=strlen(s);
for(j=0;j<t;j++)
{
if('0'<=s[j] && s[j]<='9')
{
printf("%c",s[j]);
r=0;
}
else 
{
if('0'<=s[j-1] && s[j-1]<='9')
{
printf("\n");
if(j+1!=t)
printf("\n");
}
}
}
if(r!=0)
{
printf("no phone numbers!\n");
}
if('0'<=s[t-1] && s[t-1]<='9')
printf("\n");
}
    return 0; 
}

[解决办法]

#include<stdio.h>
#include<string.h>
int main() 
{
int n,i,r,t,j;
char s[800];
scanf("%d",&n);
getchar();
for(i=0;i<n;i++)
{
r=1;
gets(s);
t=strlen(s);
for(j=0;j<t;j++)
{
if('0'<=s[j] && s[j]<='9')
{
printf("%c",s[j]);
r=0;
}
else 
{
if('0'<=s[j-1] && s[j-1]<='9')
{
//printf("\n");
if(j+1!=t)
printf("\n");
}
}
}
if(r!=0)
{
printf("no phone numbers!\n");
}
if('0'<=s[t-1] && s[t-1]<='9')
printf("\n");
}
getchar();
return 0; 
}
题目貌似不清楚 phone1213123  这样提取出1213123算电话号码吗?  楼主方法是把字符串中数字提取出来 貌似有点小错误:多了个printf("\n");
[解决办法]
自己再修改下

#include<stdio.h>
#include<string.h>
int main() 
{
int n,i,r,t,j,count1,count2=0;
char s[800];
char (*p)[800];
scanf("%d",&n);
getchar();
p=(char *)malloc(n*800);
for(i=0,count1=0;i<n;i++)
{
r=1;
gets(s);
t=strlen(s);
for(j=0;j<t;j++)
{
if('0'<=s[j] && s[j]<='9')
{
p[count1][count2++]=s[j];
r=0;
if(j+1==t)
{
p[count1][count2]='\0';
count2=0;
count1++;
}
}
else 
{
if('0'<=s[j-1] && s[j-1]<='9')
{
p[count1][count2]='\0';
count2=0;
count1++;
}
}
}
if(r!=0)
{
printf("no phone numbers!\n");
}
p[count1][count2]='\0';
}
for(i=0;i<count1;i++)
{
printf("%s\n",p[i]);
}
    return 0; 
}

热点排行