编程题:删除字符串中的除字母外的字符
输入一个字符串: noob4#$1dream 输出: noobdream
#include <stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include <windows.h> #define N 50 int main(void) { char string[N] = {0}; puts("请输入字符串"); gets(string); int n = strlen(string); string[n] = '\0'; int k=0; while (string[k] != '\0') { if ((string[k] >= 'a' && string[k] <= 'z') || (string[k] >= 'A' && string[k] <= 'Z')) { } else { for (int j = k; j < n; j++) { string[j] = string[j + 1]; } n--; k--; } k++; } puts("英语字符为:"); puts(string);
return 0; }
#include<iostream>
using namespace std;
int lengthOfString(char * string)
{
if(*string)
return 1+lengthOfString(string+1);
}else
return 0;
}
int main()
char source[1024]={0};
cin>>source;
int length=lengthOfString(source);
int k=0;
for(int i=0;i<length;i++)
if(source[i]>='a' && source[i]<='z'|| source[i]>='A' && source[i]<='Z' )
source[k++]=source[i];
source[k]='\0';
cout<<source<<endl;
#include <stdi...
用户登录可进行刷题及查看答案
#include <stdio.h> #include <string.h>
int main() { char line[100]; int i,j,len; printf("输入一个字符串: "); scanf("%s",line); len = strlen(line); for(i=0;i<len+1;i++) { if((line[i]>='a'&&line[i]<='z') || (line[i]>='A'&&line[i]<='Z')) continue; for(j=i;j<len;j++) { line[j] = line[j+1]; } len--; i--; } //line[len]='\0'; printf("%s\n",line); return 0; }
登录后提交答案