用数组或指针做函数参数,将字符串中的非小写字母删除,主函数中输入字符串,分别输出删除字符前后的字符串

2024-11-09 10:48:42
推荐回答(2个)
回答1:

#include
#include

void DeleteLower( char acstr[], int iCount )
{
int iTemp = 0;
char acTemp[30] = {0};

printf("您输入的字符串:%s\n",acstr);

for ( int i = 0; i < iCount; ++i )
{
if ( !(islower(acstr[i])) )
{
acTemp[iTemp] = acstr[i];
iTemp++;
}
}

printf("删除后的字符串:%s\n",acTemp);

}

void main()
{
printf("请输入字符串(小于30)\n");

char acstr[30] = {0};

scanf("%s",acstr);

DeleteLower(acstr,30);

}

回答2:

这是用C++写的
void work(char str[])
{
int len=strlen(str),i,j;
char *ans=new char[len+1];
for(i=0,j=0;i {
if( islower( str[i] ) )
ans[j++]=str[i];
}
for(i=0;i str[i]=ans[i];
str[i]='\0';
delete ans;
}