㈠ 編寫程序實現功能:用戶從鍵盤輸入一行字元,分別統計出其英文字母和數字字元的個數
代碼如下:
s=input("請輸入一行字元: ")
alpha,num,space,other=0,0,0,0
for i in s:
if i.isalpha():
alpha+=1
elif i.isdigit():
num+=1
elif i.isspace():
space+=1
else:
other+=1
print('英文字元數{},數字字元數{},空格字元數{},其他字元數{}'.format(alpha,num,space,other))
(1)編一個程序輸入一行字元什麼意思擴展閱讀
字元串的函數應用:
1、連接運算 concat(s1,s2,s3…sn) 相當於s1+s2+s3+…+sn.
例:concat(『11』,'aa』)='11aa』;
2、求子串。 Copy(s,I,I) 從字元串s中截取第I個字元開始後的長度為l的子串。
例:(『abdag』,2,3)=』bda』
3、刪除子串。過程 Delete(s,I,l) 從字元串s中刪除第I個字元開始後的長度為l的子串。
例:s:=』abcde』;delete(s,2,3);結果s:=』ae』
4、插入子串。 過程Insert(s1,s2,I) 把s1插入到s2的第I個位置
例:s:=abc;insert(『12』,s,2);結果s:=』a12bc』
5、求字元串長度 length(s) 例:length(『12abc』)=5
在ASP中 求字元串長度用 len(s)例: len("abc12")=5
6、搜索子串的位置 pos(s1,s2) 如果s1是s2的子串 ,則返回s1的第一個字元在s2中的位置,若不是子串,則返回0.
例:pos(『ab』,』12abcd』)=3
㈡ 編寫一段C語言程序,程序功能是: 輸入一行字元,分別統計其中英文字母、空格、數字和其它字元的個數並
#include<stdio.h>
int main()
{ int zm=0,kg=0,sz=0,qt=0;
char c;
while((c=getchar())!=' ')
if(c>='A' && c<='Z' || c>='a' && c<='z')
zm++;
else if(c==' ')
kg++;
else if(c>='0' && c<='9')
sz++;
else
qt++;
printf("英文字母:%d ",zm);
printf("空格:%d ",kg);
printf("數字:%d ",sz);
printf("其它:%d ",qt);
return 0;
}
㈢ 編寫一個程序,輸入一行字元,統計其中26個英文字母(不區分大小寫)各出現多少次
#include<iostream>
usingnamespacestd
int main()
{
void countChar(char*str,char*a);
char str[30];
int a[26]={0};
cout<<"pleaseinputastring:";
cin.get(str,30);//這里未檢查數組越界,別輸入太多會崩潰
countChar(str,a);
return0;
}
voidcountChar(char*str,char*a)
{
while(*str)
{
a[*str-'a']++;//a[0]對應保存a出現的次數,如果*str為字元a那麼*str-'a'=0
//同樣如果*str為字元b那麼'b'-'a'=1,則a[1]++,來記錄b出現的次數
*str++;
}
for(inti=0;i<26;i++)//數組中的數字就是對應字母的個數
cout<<(char)('a'+i)<<a[i]<<endl;
}
include用法:
#include命令預處理命令的一種,預處理命令可以將別的源代碼內容插入到所指定的位置;可以標識出只有在特定條件下才會被編譯的某一段程序代碼;可以定義類似標識符功能的宏,在編譯時,預處理器會用別的文本取代該宏。
插入頭文件的內容
#include命令告訴預處理器將指定頭文件的內容插入到預處理器命令的相應位置。有兩種方式可以指定插入頭文件:
1、#include<文件名>
2、#include"文件名"
㈣ 編寫一個程序,其功能為:從鍵盤輸入一行字元,分別統計其中字母、字元、空格以及其他字元的個數
我是用java編的你看一下:
import java.io.BufferedReader;
import java.util.Scanner;
//編寫一個程序,其功能為:從鍵盤輸入一行字元,分別統計其中字母、字元、空格以及其他字元的個數
public class Test{
public static void main(String[] args){
int letter = 0;//記錄字母的個數
int chars = 0;//記錄字元的個數
int space = 0;//記錄空格的個數
int others = 0;//記錄其他字元的個數
Scanner in = new Scanner(System.in);
String str = null;
if(in.hasNextLine()){
str = in.nextLine();
}
else{
System.out.println("Error");
System.exit(-1);
}
char[] array = str.toCharArray();
for(char i:array){
if(Character.isLetter(i)) {letter++;}
if(Character.isWhitespace(i)) {space++;}
if(Character.isDefined(i)) {chars++;}
else {others++;}
}
System.out.println("字母的個數為:"+letter);
System.out.println("空格的個數為:"+space);
System.out.println("字元的個數為:"+chars);
System.out.println("其他個數為: "+others);
}
}
㈤ JAVA程序 輸入一行字元
public static void main(String[] args) {
System.out.println("你輸入的單詞數為:"+args.length);
StringBuffer strbuffer = new StringBuffer();
for(int i=0;i<args.length;i++){
String str = args[i];
strbuffer.append(str+" ");
System.out.println("正向");
System.out.println(str);
System.out.println("反向");
for(int j=str.length()-1;j>=0;j--){
System.out.print(str.charAt(j));
}
System.out.println();
}
StringBuffer view = new StringBuffer();
for(int i=0;i<strbuffer.length();i++){
String a = strbuffer.charAt(i)+"";
int counter = 0;
for(int j=0;j<strbuffer.length();j++){
if(a.equals(strbuffer.charAt(j)+"")){
counter++;
}
}
System.out.println("字元"+a+"出現的位置"+i+"出現的次數"+counter);
}
}
㈥ 編寫一個程序,輸入一行字元,以回車結束,分別統計出其中的英文字母、空格、數字和其他字元的數。
#include <stdio.h>
int main()
{
int letter=0,space=0,digit=0,others=0;
char c;
while((c=getchar())!=' '){
if(c==' ')
space++;
else if(c>='1'&&c<='9')
digit++;
else if((c>='a'&&c<='z')||c>='A'&&c<='Z')
letter++;
else others++;
}
printf("The number of letters is:%d ",letter);
printf("The number of spaces is:%d ",space);
printf("The number of digits is:%d ",digit);
printf("The number of other words is:%d ",others);
return 0;
}
(6)編一個程序輸入一行字元什麼意思擴展閱讀:
字元包括字母、數字、運算符號、標點符號和其他符號,以及一些功能性符號。字元在計算機內存放,應規定相應的代表字元的二進制代碼。代碼的選用要與有關外圍設備的規格取得一致。這些外圍設備包括鍵盤控制台的輸入輸出、列印機的輸出等等。
字元作輸入時,要自動轉換為二進制代碼存於機內;輸出時,計算機內二進制代碼自動轉化為字元,兩者的轉換全是靠外圍設備實現的。字元是數據結構中最小的數據存取單位。
㈦ 編寫程序,輸入一行數字字元,回車結束,每個數字字元的前後都有空格,把這一行中的數字轉換成一個整數。
while((c=getchar())!='\n') //一個一個字元接受你輸入的字元直到回車
//(雖然你是一次性輸入回車這個程序是一個一個字元接受的)
{if(c>='0' && c<='9') n=n*10+c-'0';}
//if(c>='0' && c<='9') 判斷字元是不是數字字元
//n=n*10+c-'0';--c-'0'把字元數字轉換成整型數字
//這個程序要先把n初始化為0;
㈧ 請編寫程序,輸入一行字元(用回車結束),輸出每個字元以及對應的ASCII代碼值,每行輸出三對。
給n賦值0就行了,修改如下:
#include<stdio.h>
main()
{
char ch;
int n=0;
while((ch=getchar())!='\n')
{
printf("%c:%d",ch,ch);
n++;
if(n%3==0)
putchar('\n');
}
}
n++的位置也改了下,要不第一行是空行