编程题:删除字符串中的除字母外的字符
输入一个字符串: noob4#$1dream 输出: noobdream
#include <stdio.h> #include <string.h>
void removeNonLetters(char *str) { int i = 0, j = 0; while (str[i] != '\0') { // 只保留字母 if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z')) { str[j++] = str[i]; } i++; } str[j] = '\0'; // 添加字符串结束符 }
int main() { char str[100]; printf("请输入一个字符串: "); scanf("%s", str);
removeNonLetters(str); printf("处理后的字符串: %s\n", str); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { vector<char> arr; char c; // 读取所有字符(包括空格,直到换行符) while ((c = cin.get()) != '\n') { arr.push_back(c); } vector<char> result; for (int i = 0; i < arr.size(); i++) { if ((arr[i] >= 'a' && arr[i] <= 'z') || (arr[i] >= 'A' && arr[i] <= 'Z')) { result.push_back(arr[i]);//字符不能简单的赋值 } } for(char v :result){ cout << v; } return 0; }
1
#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...
用户登录可进行刷题及查看答案
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; }
登录后提交答案