① 尋找一個c語言程序:從輸入中過濾字元串
可以直接定義一個字元類型,然後用gets(),從鍵盤得到這個字元串,再這個字元串進行遍版歷解析權
下面提供一個例子:從鍵盤輸出一串字元並且輸出其中的數字
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
intmain(void){
charstring[100];
inti;//循環變數
gets(string);
for(i=0;i<strlen(string);i++){
if((string[i]>='0')&&(string[i]<='9')){//判斷每個字元是否是0~9直接的數
printf("%c",string[i]);//是數字的話直接列印
}
}
puts("");
puts("end");
returnEXIT_SUCCESS;
}
② 幫改下錯誤關於輸入一個字元串,過濾此串,濾掉字母字元,並統計新生
||這不是廢話嗎……
azAZ的編碼分別是97 122 65 90
根據你的代碼(*(ptr+i)>='z'|| *(ptr+i)<='a'||*(ptr+i)>='Z' || *(ptr+i)<='A'),也就是說要版比122大或比97小,但是,在這權里被排除的a-z(97-122)都大於等於Z!
同理,A-Z都小於等於a……
應改為((*(ptr+i)>='z'&& *(ptr+i)<='a')||(*(ptr+i)>='Z'&&*(ptr+i)<='A'))
另外,一般來說函數必須放在main的上面,而且for(i=0,j=0;*(ptr+i)!="\0";i++) 中的雙引號應該改成單引號……
③ C++程序設計:從考試文件夾下的文件「data.txt 」中讀入一個字元串,過濾此串,只保留串中的字母字元
#include<iostream>
#include<fstream>
#include<string>
usingnamespacestd;
intmain()
{
fstreamfile1("data.txt",ios::in),file2("example1.cpp",ios::out);
stringdata;
stringresult;
file1>>data;
for(inti=0;i<data.size();i++)
if(isalpha(data[i]))
result+=data[i];
cout<<result<<"size:"<<result.size()<<endl;
file1.close();
file2<<result;
file2.close();
}
④ C#編程 輸入一個字元串,過濾此串,只保留串中的字母字元,並統計新生成串中包含的字母個數。
string s = "fds23jfdslf323";
string newStr = "";
for(int i = 0; i < s.Length; i++)
{
int tmp = (int)s[i];
if((tmp >= 65 && tmp <= 90) || (tmp >= 97 && tmp <= 122))
{
newStr += s[i];
}
}
最後可以用newStr.Length 來獲取新字元串的字母個數,因為這個字元串中,肯內定全是字母啦容。
⑤ 任意輸入一個字元串和字元,要求從該字元串中刪除指定字元
#include <iostream>
using namespace std;
int main()
{
char c[100],x;
char *a=c;
cin>>c;
cout<<"輸入抄要刪除的襲東西\n";
cin>>x;
for(;*a;a++)
if(*a==x)
{
for(int i=0;*(a+i);i++)
*(a+i) = *(a+i+1);a--;
}
cout<<c;
}
}
這樣可以完成刪除指定字元的工作
⑥ 急急急這個代碼是輸入一個字元串過濾此串只保留非數字嗎
輸出輸入中的數字
⑦ 輸入一個字元串,過濾此串,只保留串中的字母字元,並統計新生成串中包含字母的個數
||代碼如下:
#include<stdio.h>
#include<stdlib.h>
intmain()
{
charstr[100],*p=str,*q=str,cnt=0;
scanf("%s",str);
while(*p!='