c语言从键盘输入一字符串 C语言编程:从键盘中输入一个英文字符串
C\u8bed\u8a00\u7f16\u7a0b \u7531\u952e\u76d8\u4efb\u610f\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\u548c\u4efb\u4e00\u4e2a\u5b57\u7b26\uff0c\u8981\u6c42\u4ece\u8be5\u5b57\u7b26\u4e32\u4e2d\u5220\u9664\u6240\u6307\u5b9a\u7684\u5b57\u7b26#include
int main(void)
{
int i, j = 0;
char ch;
char str[100];
printf("\u8bf7\u8f93\u5165\u5b57\u7b26\u4e32:\n");
scanf("%s", str);
printf("\u8bf7\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26:\n");
scanf(" %c", &ch);
for (i=0; str[i]!='\0'; i++)
{
if (str[i] != ch)
{
str[j++] = str[i];
}
}
str[j] = '\0';
printf("%s\n", str);
return 0;
}
#include
#include
int main()
{
int strSize = 100;
char *str = (char *)malloc(sizeof(char) * strSize);
int charNum = 0;
char input;
//\u9010\u4e2a\u5b57\u7b26\u8f93\u5165\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u8f93\u5165int\u53ef\u4ee5\u8868\u793a\u7684\u6700\u5927\u503c\u4e2a\u5b57\u7b26
printf("\u8bf7\u8f93\u5165\u4efb\u610f\u4e2a\u5b57\u7b26:\n");
while(true)
{
scanf("%c",&input);
if(input != '#')
{
if((input >= 'A' && input = 'a' && input <= 'z'))
{
if(charNum > strSize)
{
strSize += 100;
str = (char *)realloc(str,strSize);
}
str[charNum] = input;
charNum++;
}
}
else
{
break;
}
}
//\u8f93\u5165\u7ed3\u679c\u5206\u6790
int i = 0,j = 0;
char *tempChar = (char *)malloc(sizeof(char) * charNum);
int *tempCharNum = (int *)malloc(sizeof(int) * charNum);
int charType = 0;
bool exist = false;
for(i = 0; i < charNum; i++)
{
exist = false;
tempChar[i] = '#';
tempCharNum[i] = 0;
for(j = 0; j < charNum; j++)
{
if(tempChar[j] == '#')
{
break;
}
if(tempChar[j] == str[i])
{
exist = true;
tempCharNum[j] += 1;
}
}
if(exist == false)
{
tempChar[charType] = str[i];
tempCharNum[charType] = 1;
charType++;
}
}
int t1;
char t2;
for(j = 0; j < charType - 1; j++)
{
for(i = 0; i < charType; i++)
if(tempCharNum[i] > tempCharNum[i+1])//\u5982\u679ca[i]\u5927\u4e8ea[i+1]
{
//\u4ea4\u6362a[i]\u548ca[i+1]\u7684\u503c\uff0c\u5373\u628a\u8f83\u5927\u7684\u5143\u7d20\u5f80\u540e\u6392
t1 = tempCharNum[i];
tempCharNum[i] = tempCharNum[i+1];
tempCharNum[i+1] = t1;
t2 = tempChar[i];
tempChar[i] = tempChar[i+1];
tempChar[i+1] = t2;
}
}
for(i = 0; i < charNum; i++)
{
if(tempChar[i] != '#')
{
printf("\u5355\u8bcd:%c,\u6b21\u6570:%d\n",tempChar[i],tempCharNum[i]);
}
}
free(str);
free(tempChar);
free(tempCharNum);
return 0;
}
int main()
{
char str[100];
long long an[100];
int cnt=0,i=0,f=0;
printf("Please input a string:");
gets(str);
while(str[i])
{
if(str[i]>='0'&&str[i]<='9')
{
if(f==1)
{
an[cnt]=an[cnt]*10+str[i]-'0';
}
if(f==0)
{
cnt++;
f=1;
an[cnt]=str[i]-'0';
}
}
else
{
f=0;
}
i++;
}
if(cnt==0)
{
printf("None\n");
}
else
{
printf("Total number: %d\n",cnt);
for(i=1;i<=cnt;i++)
{
printf("%lld*2=%lld\n",an[i],an[i]*2);
}
}
return 0;
}
#include "stdio.h"
void main()
{
char str[81];
long num[40];
long n;
int cnt=0;
int i;
gets(str);
for(i=0;str[i];i++)
{
n=0;
if(str[i]>='0' && str[i]<='9')
{
while(str[i] && str[i]>='0' && str[i]<='9')
{
n=n*10+str[i]-'0';
i++;
}
num[cnt++]=n*2;
}
}
if(cnt)
{
printf("%d个数\n",cnt);
for(i=0;i<cnt;i++)
printf("%ld ",num[i]);
}
else
printf("None.\n");
}
#include<stdio.h>
void print_num(char *str)
{
char string[20][20];
int i=0,num=0,new_num=0,counter=0;
while(*(str+i))//即*(str+i)!='\0'
{
if(*(str+i)>='0'&&*(str+i)<='9'&&new_num==0)
{
string[num][counter]=*(str+i);
counter++;
new_num=1;
}
else if(*(str+i)>='0'&&*(str+i)<='9'&&new_num==1)
{
string[num][counter]=*(str+i);
counter++;
}
else if((*(str+i)<'0'||*(str+i)>'9')&&new_num==1)
{
string[num][counter]='\0';
new_num=0;
num++;
counter=0;
}
else if((*(str+i)<'0'||*(str+i)>'9')&&new_num==0);
i++;
}
string[num][counter]='\0';
num=num+1;
printf("there are %d num.\nthere are:\n",num);
for(i=0;i<num;i++)
puts(string[i]);
}
int main()
{
char string[100],*str;
printf("Please enter a string:\n");
gets(string);
str=string;
print_num(str);
return 0;
}
至于每个整数*2,可以作为练习来做,可以好好看一下这个代码,值得思考的
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str[80];
int i;
int j = 0;
int nFlag = 0;
int count = 0;
int a[20];
scanf("%s", str);
for (i = 0; i < strlen(str); i++)
{
if (str[i] >= '0' && str[i] <= '9' && nFlag == 0)
{
count++;
a[j] = atoi(str + i);
j++;
nFlag = 1;
}
else
{
nFlag = 0;
}
}
if (count == 0)
{
printf("None\n");
}
printf("%d ", count);
for (i = 0; i < j; i++)
{
printf("%d ", 2 * a[i]);
}
}
/*
12 31 534 77 91
2 × 12 = 24
2 × 31 = 62
2 × 534 = 1068
2 × 77 = 154
2 × 91 = 182
n
n = 5
Press any key to continue
*/
#include <stdio.h>
int main() {
int num,n = 0;
while(scanf("%d",&num) == 1) {
++n;
printf("2 × %d = %d\n",num,2*num);
}
if(n == 0) printf("None.\n");
else printf("\nn = %d\n",n);
return 0;
}
绛旓細c璇█浠庨敭鐩樿緭鍏ュ瓧绗︿覆鐨勬楠ゅ涓嬶細1.棣栧厛鎴戜滑鏂板缓涓涓猟ev C++鐨勯」鐩2.鎺ヤ笅鏉ュ湪椤圭洰涓柊寤篊璇█绋嬪簭鏂囦欢銆3.鐒跺悗鍦–璇█鏂囦欢涓0鏄庝竴涓瓧鑺傛暟缁勩4.鎺ヤ笅鏉ユ垜浠氳繃printf鍑芥暟鎻愮ず鐢ㄦ埛杈撳叆瀛楃涓诧紝閫氳繃scanf鎺ユ敹鐢ㄦ埛杈撳叆鐨勫瓧绗︿覆銆5.鎺ョ潃鍦ㄩ氳繃printf鍑芥暟杈撳嚭鐢ㄦ埛杈撳叆鐨勫唴瀹广6.鏈鍚庤繍琛孋璇█绋嬪簭锛屾垜浠...
绛旓細鍦–璇█涓紝鏈変袱涓嚱鏁板彲浠ヨ鐢ㄦ埛浠庨敭鐩樹笂杈撳叆瀛楃涓诧紝瀹冧滑鍒嗗埆鏄細scanf()锛氶氳繃鏍煎紡鎺у埗绗%s杈撳叆瀛楃涓銆傞櫎浜嗗瓧绗︿覆锛宻canf() 杩樿兘杈撳叆鍏朵粬绫诲瀷鐨勬暟鎹俫ets()锛氱洿鎺ヨ緭鍏ュ瓧绗︿覆锛屽苟涓斿彧鑳借緭鍏ュ瓧绗︿覆銆備絾鏄紝scanf() 鍜 gets() 鏄湁鍖哄埆鐨勶細scanf() 璇诲彇瀛楃涓叉椂浠ョ┖鏍间负鍒嗛殧锛岄亣鍒扮┖鏍煎氨璁や负褰撳墠瀛...
绛旓細鍦–璇█涓疄鐜扮敤閿洏杈撳叆涓涓瓧绗︿覆锛屽苟鎵撳嵃鍑烘潵鐨勮В鍐虫柟娉曞涓嬶細1銆侀鍏堝畾涔変竴涓瓧绗︿覆鏁扮粍锛屽浣犲彲浠ョ粰瀛楃涓100涓暟缁勭┖闂銆傚鍥炬嫭鍙峰唴鏁板瓧鍗充负浣犲畾涔夌殑鏁扮粍澶у皬銆2銆乬ets()鍑芥暟鐢ㄦ潵鑾峰彇閿洏杈撳叆鐨勫瓧绗︿覆锛屾嫭鍙峰唴鐨剆鍗充负瀛楃涓插彉閲忋3銆佹帴鐫鐢╬uts锛堬級鍑芥暟杈撳嚭涔嬪墠鎴戜滑浠庨敭鐩樿鍙栫殑瀛楃涓层傛嫭鍙峰唴涓...
绛旓細鐢╯canf/printf杈撳叆杈撳嚭銆俿canf/printf鏄疌璇█鐨勬牸寮忓寲杈撳叆杈撳嚭鍑芥暟锛屽彲浠ョ敤浜庤緭鍏ヨ緭鍑哄悇绉嶅熀纭绫诲瀷锛屽瓧绗︿覆(char*)涔熷湪鍏舵敮鎸佽寖鍥村唴銆傛牸寮忓寲瀛楃涓%s銆傚畾涔 char str[100];杈撳叆 scanf("%s",str);杈撳嚭 printf("%s",str);2 C璇█杩樻敮鎸佷笓闂ㄧ殑瀛楃涓茶緭鍏ヨ緭鍑哄嚱鏁帮細gets/puts銆俫ets鐨勪綔鐢ㄤ负锛岃鍏ヤ竴琛岃緭...
绛旓細include<stdio.h>char toupper(char c){ if(c>='a'&&c<='z')c-=32; return c;}int main(){ char s[200]; int i; gets(s); for(i=0; s[i]; i++) s[i]=toupper(s[i]); printf("%s\n",s); return 0;} ...
绛旓細1銆侀鍏堥渶瑕佹墦寮缂栫▼杞欢銆2銆杈撳叆浠ヤ笅绋嬪簭锛#include <stdio.h>int main(){ char str[40]; scanf("%s",&str); printf("%s",str); return 0;}銆3銆佺劧鍚庢寜F5杩愯绋嬪簭銆4銆佺劧鍚庤緭鍏ユ兂瑕佽緭鍏ョ殑瀛楃涓銆5銆佹寜鍥炶溅閿紝灏变細寮瑰嚭浣犺緭鍏ョ殑瀛楃涓层6銆佹敞鎰忓瓧绗︿覆鐨勫ぇ灏忥紝鏀瑰彉涓嫭鍙蜂腑鐨勬暟瀛楁敼鍙樿緭鍏...
绛旓細include <stdio.h> int main() { char str[] = "hello, world!";int len = sizeof(str) / sizeof(char); // 璁$畻瀛楃涓鐨勯暱搴 for (int i = 0; i < len; i++) { printf("%c ", str[i]); // 閫愪釜杈撳嚭瀛楃涓蹭腑鐨勫瓧绗 } return 0;} ...
绛旓細鍦c璇█閲岄潰杈撳叆瀛楃涓鏈変袱绉嶆柟寮1锛鐢gets(),char a[1000];gets(a);2:鍙互鐢╢or璇彞寰幆锛宖or锛坕=0;i<100;i++锛塻canf("%c",a[i]);
绛旓細鍦C璇█涓緭鍏ュ瓧绗︿覆鍚庣劧鍚庢墦鍗板嚭鏉ョ殑缂栫▼鏂规硶濡備笅锛1.棣栧厛闇瑕佸厛瀹氫箟涓涓瓨鏀惧瓧绗︿覆鐨勬暟缁勭┖闂达紝濡傜粰瀛楃涓100涓暟缁勭┖闂淬2.鎺ョ潃浣跨敤gets()鍑芥暟鏉ヨ幏鍙閿洏杈撳叆鐨勫瓧绗︿覆銆備竴鑸牸寮忎负gets(瀹氫箟鐨勫瓧绗︽暟缁勫悕)銆3.鐒跺悗鍐嶄娇鐢╬uts()鍑芥暟鏉ヨ緭鍑轰箣鍓嶈緭鍏ョ殑瀛楃涓插唴瀹广備竴鑸牸寮弍uts锛堝畾涔夌殑瀛楃鏁扮粍鍚嶏級銆4....
绛旓細浠ヤ笅鏄C璇█绋嬪簭锛屼娇鐢ㄦ寚閽堝皢閿洏杈撳叆鐨瀛楃涓閫嗗簭瀛樻斁锛屽苟杈撳嚭缁撴灉锛歚``c include <stdio.h> include <string.h> int main() { char str[100], temp;int len, i;char *p1, *p2;printf("璇杈撳叆涓涓瓧绗︿覆锛");scanf("%s", str);len = strlen(str);p1 = str;p2 = str + len - ...