从键盘输入字符串,字符串中含大写字母,小写字母,以及其他字符。编写程序将大写字母,小写字母,其他字符按顺序分离并分别保存在3个字符数组中,原字符数组保持不变。要求:(1)用3个子函数分别实现写字母,小写字母,其他字符的分离(2)子函数形式参数为指向性字符的指针变量(3)主函数中调用三个子函数实现各个字符的分离并显示原字符及3类分离后的字符。具体参照图片。
没什么难度,好心点给你代码吧
#include
#include
void AtoZ(const char *p)
{
int i = 0;
char q[50] = {0};
while(*p != '\0')
{
if (*p >= 'A' && *p <= 'Z')
{
q[i] = *p;
i++;
}
p++;
}
q[i] = '\0';
printf("AtoZ: %s\n", q);
return;
}
void atoz(const char *p)
{
int i = 0;
char q[50] = {0};
while(*p != '\0')
{
if (*p >= 'a' && *p <= 'z')
{
q[i] = *p;
i++;
}
p++;
}
q[i] = '\0';
printf("atoz: %s\n", q);
return;
}
void other(const char *p)
{
int i = 0;
char q[50] = {0};
while(*p != '\0')
{
if (!(*p >= 'A' && *p <= 'Z')&&!(*p >= 'a' && *p <= 'z'))
{
q[i] = *p;
i++;
}
p++;
}
q[i] = '\0';
printf("other: %s\n", q);
return;
}
int main(int argc, char const *argv[])
{
char str[50] = {0};
printf("please input any string you want!\n");
scanf("%s",str);
getchar();
char *p = str;
//printf("cut out 'A' to 'Z'\n");
AtoZ(p);
//printf("cut out 'a' to 'z'\n");
atoz(p);
//printf("cut out the other\n");
other(p);
return 0;
}
可以使用正则表达式
1、标准的C和C++都不支持正则表达式,但有一些函数库可以辅助C/C++程序员完成这一功能,其中最著名的当数Philip Hazel的Perl-Compatible Regular Expression库,许多Linux发行版本都带有这个函数库。
2、C/C++ 中使用正则表达式一般分为三步:
1)编译正则表达式 regcomp()
int regcomp (regex_t *compiled, const char *pattern, int cflags)
这个函数把指定的正则表达式pattern编译成一种特定的数据格式compiled,这样可以使匹配更有效。函数regexec 会使用这个数据在目标文本串中进行模式匹配。执行成功返回0。
2)匹配正则表达式 regexec()
int regexec (regex_t *compiled, char *string, size_t nmatch, regmatch_t matchptr [], int eflags)
当编译好正则表达式后,就可以用regexec 匹配我们的目标文本串了,如果在编译正则表达式的时候没有指定cflags的参数为REG_NEWLINE,则默认情况下是忽略换行符的,也就是把整个文本串当作一个字符串处理。执行成功返回0。
3)释放正则表达式 regfree()
void regfree (regex_t *compiled)
当使用完编译好的正则表达式后,或者要重新编译其他正则表达式的时候,可以用这个函数清空compiled指向的regex_t结构体的内容。请注意,如果是重新编译的话,一定要先清空regex_t结构体。
获取每个字符的16进制, A~Z 和 a~z 的16进制都是连续的,对应 ASCII 表, 依据16进制判断是那种字符
没有难度的题,不仅你们不愿意写,大神们也不愿意写。:)
用不到正则表达式的,那是杀鸡用牛刀。
三个 条件 'Z' >= x >= 'A' 和 'z' >=x >'a' 另外一个就是 other
输出就用一个数组(初始化为0),然后用一个计数器,找到一个就加一。
char p="AD12avbc&)aAc";
char szMax[10]={0};
char szMin[10]={0};
char szStr[10]={0};
int iMaxCount=0;
int iminCount=0;
int istrCount=0;
for(int i=0;i {
if(p[i]>='A' &&(int)p[i]<='Z')
{
szMax[iMaxCount++]=p[i];
}
else if(p[i]>='a'&&(int)p[i]<'z')
{
szMin[iminCount++]=p[i];
}
else if(p[i]>='1' &&p[i]<='9')
{
}
else
{
szStr[istrCount++]=p[i];
}
}