導航:首頁 > 編程語言 > 字元串c語言編程題

字元串c語言編程題

發布時間:2022-07-05 12:03:06

⑴ c語言編程題!字元串復制。輸入一個字元串,把它復制到一個字元數組中,並輸出。

#include <stdio.h>

int main()

{

char a[100] = "",b[100] = "";

int i = 0;

printf("輸入一行字元: ");

scanf("%s",a);

for(i = 0;a[i] != '';i++){

b[i] = a[i];

}

printf("%s ",b);

return 0;

}

(1)字元串c語言編程題擴展閱讀:

使用scanf()函數需要注意的問題:

1.對於字元串數組或字元串指針變數,由於數組名可以轉換為數組和指針變數名本身就是地址,因此使用scanf()函數時,不需要在它們前面加上"&"操作符。

2.可以在格式化字元串中的"%"各格式化規定符之間加入一個整數,表示任何讀操作中的最大位數。

3.scanf函數中沒有類似printf的精度控制。

如:scanf("%5.2f",&a); 是非法的。不能企圖用此語句輸入小數為2位的實數。

4.scanf中要求給出變數地址,如給出變數名則會出錯

如:scanf("%d",a);是非法的,應改為scanf("%d",&a);才是合法的。

5.在輸入多個數值數據時,若格式控制串中沒有非格式字元作輸入數據之間的間隔,則可用空格,TAB或回車作間隔。

C編譯在碰到空格,TAB,回車或非法數據(如對「%d」輸入「12A」時,A即為非法數據)時即認為該數據結束。

6.在輸入字元數據(%c)時,若格式控制串中無非格式字元,則認為所有輸入的字元均為有效字元。

⑵ 求一個關於字元串的c語言編程題目,謝謝

#include<stdio.h>
#include<stdlib.h>

intmain()
{
charstr[1000];
gets(str);//默認輸入中沒有換行,按行輸入所有字元
inti,j,max;
for(i=0;i<strlen(str);++i){
if(str[i]>='0'&&str[i]<='9'){//讀到一個數字後就假設其後還有數字並尋找最大的數值,存入max,直到數字結束
max=str[i++]-'0';
while(1){
if(str[i]>='0'&&str[i]<='9'&&i<strlen(str)){
max=(str[i]-'0')>max?(str[i]-'0'):max;
i++;
}
else
break;
}
while(1){//數字結束後從後一個字元開始找大寫字母
if(i<strlen(str)){//防止數組越界
if(str[i]>='A'&&str[i]<='Z'){//找到大寫字母後開始輸出
printf("%c",str[i++]);
for(j=1;j<max;++j){//輸出字元數大於max或遇到其它字元時停止,並換行
if(str[i]>='a'&&str[i]<='z'){
printf("%c",str[i++]);
}
else{
printf(" ");
break;
}
}
if(j==max)printf(" ");
}
elseif(str[i]>='0'&&str[i]<='9')
break;
else
i++;
}
else
break;
}
}
}
return0;
}


也不是完全理解你的意思,但至少樣例能跑出來。這段代碼建立在這樣的基礎上:

(1) 輸入的字元串沒有換行符;

(2) 發現一個大寫字母後輸出的是它之後的從a~z的字母,不包含其它。


你可以根據你的意思跑點例子,發現問題再告訴我,可以改,有問題可追問。

⑶ C語言 關於字元串處理的程序題知道

#include
<stdio.h>
#include<string.h>
#include<stdlib.h>
struct
STKY
{
char
keyword[100];
int
count;
float
frq;//出現頻率
struct
STKY
*next;
}*h;//帶頭結點的單鏈表,頭結點h->count中保留了句子中的單詞個數,其他節點count保存的是keyword出現的次數
int
AddList(struct
STKY
*h,char
*szText)
{
struct
STKY
*p,*q;
char
szTemp[50]={0};
int
i=0,j=0;;
h->count=1;
while(szText[i])
{
if(szText[i]!='
')
szTemp[j++]=szText[i];
else
{
h->count++;
szTemp[j]='\0';
p=q=h;
if(p->next==NULL)
{
p=(struct
STKY*)malloc(sizeof(struct
STKY));
p->next=NULL;
strcpy(p->keyword,szTemp);
p->count=1;
q->next=p;
p=q;
}
else
{
p=q=h->next;
while(p)
{
if(strcmp(p->keyword,szTemp)==0)
{
p->count++;
break;
}
q=p;
p=p->next;
}
if(p==NULL)
{
p=(struct
STKY*)malloc(sizeof(struct
STKY));
p->next=NULL;
strcpy(p->keyword,szTemp);
p->co...
#include
<stdio.h>
#include<string.h>
#include<stdlib.h>
struct
STKY
{
char
keyword[100];
int
count;
float
frq;//出現頻率
struct
STKY
*next;
}*h;//帶頭結點的單鏈表,頭結點h->count中保留了句子中的單詞個數,其他節點count保存的是keyword出現的次數
int
AddList(struct
STKY
*h,char
*szText)
{
struct
STKY
*p,*q;
char
szTemp[50]={0};
int
i=0,j=0;;
h->count=1;
while(szText[i])
{
if(szText[i]!='
')
szTemp[j++]=szText[i];
else
{
h->count++;
szTemp[j]='\0';
p=q=h;
if(p->next==NULL)
{
p=(struct
STKY*)malloc(sizeof(struct
STKY));
p->next=NULL;
strcpy(p->keyword,szTemp);
p->count=1;
q->next=p;
p=q;
}
else
{
p=q=h->next;
while(p)
{
if(strcmp(p->keyword,szTemp)==0)
{
p->count++;
break;
}
q=p;
p=p->next;
}
if(p==NULL)
{
p=(struct
STKY*)malloc(sizeof(struct
STKY));
p->next=NULL;
strcpy(p->keyword,szTemp);
p->count=1;
q->next=p;
p=q;
}
}
j=0;
}
i++;
}
////////////////////計算出現頻率
p=q=h->next;
if(p==NULL)
return
1;
while(p)
{
p->frq=(float)(p->count*1.0/h->count);
p=p->next;
}
return
0;
}
int
main()
{
char
szText[1024]="good
morning
just
have
a
good
breakfast
";//測試的字元串
char
szFormat[1024]={0};
int
i=0,j=0;
int
iPrev=0;
struct
STKY
*p;
while(szText[i])
{
if(szText[i]!='
')
{
szFormat[j++]=szText[i];
iPrev=0;
}
else
{
if(iPrev==0)
szFormat[j++]=szText[i];
iPrev=1;
}
i++;
}
if(szFormat[j-1]=='
')
szFormat[j-1]='\0';
puts("處理前的字串:");
puts(szText);
strcpy(szText,szFormat);
puts("處理後的字串:");
puts(szText);
////////////////////////////////////////////
h=(struct
STKY*)malloc(sizeof(struct
STKY));
h->next=NULL;
AddList(h,szText);
p=h->next;
puts("計算出的單詞出現頻率");
while(p)
{
printf("%s:\n%f\n",p->keyword,p->frq);
p=p->next;
}
return
0;
}
展開

⑷ c語言字元數組和字元串編程題 求解

#include <stdio.h>

int main(){

void Total(char st[],int b[]);

char st[20];

int b[4]={0,0,0,0};

scanf("%s",st);

Total(st,b);

printf("字元串長度:%d,字母個數:%d,數字個數:%d,其他字元個數:%d ",b[0],b[1],b[2],b[3]);

return 0;

}

void Total(char st[],int b[]){

int i;

while(st[i]!=''){

b[0]++;

if((st[i]>='A' && st[i]<='Z')||(st[i]>='a' && st[i]<='z')){

b[1]++;

}else if(st[i]>='0' && st[i]<='9'){

b[2]++;

}else{

b[3]++;

}

i++;

}

}

⑸ c語言編程題:定義函數con()將兩個字元串連接,主函數輸入兩個字元串,調用con()連接字元串

#include <stdio.h>

void con(char*s1,char*s2)

{int i,j;

for(i=0;s1[i];i++);

for(j=0;s1[i++]=s2[j++];);

}

int main()

{

char s1[200],s2[100];

gets(s1);

gets(s2);

con(s1,s2);

puts(s1);

return 0;

}

⑹ C語言中一個關於「字元串」的編程題。

#include
void fun(char *s)
{ int i, n, k; char c;
n=0;
for(i=0; s[i]!='\0'; i++) n++;
/**********found**********/
if(n%2==0) k=n-___1___ ;
else k=n-2;
/**********found**********/
c=___2___ ;
for(i=k-2; i>=1; i=i-2) s[i+2]=s[i];
/**********found**********/
s[1]=___3___ ;
}
main()
{ char s[80]="abcdefgh";
printf("\nThe original string is : %s\n",s);
fun(s);
printf("\nThe result is : %s\n",s);
}

自己填空吧

⑺ C語言編程題,輸入5個字元串,然後排序

#define LINEMAX 20/*定義字元串的最大長度*/

int main()

{int i;

char**p,*pstr[5],str[5][LINEMAX];

for(i=0;i&lt;5;i++)

pstr&lt;i&gt;=str&lt;i&gt;;/*將第i個字元串的首地址賦予指針數組pstr的第i個元素*/

printf("input 5 strings: ");

for(i=0;i&lt;5;i++)

scanf("%s",pstr&lt;i&gt;);

p=pstr;

sort(p);

printf("strings sorted: ");

for(i=0;i&lt;5;i++)

printf("%s ",pstr&lt;i&gt;);

}

sort(char**p)/*冒泡法對5個字元串排序函數*/

{int i,j;

char*temp;

for(i=0;i&lt;5;i++)

{for(j=i+1;j&lt;5;j++)

{if(strcmp(*(p+i),*(p+j))&gt;0)/*比較後交換字元串地址*/

{temp=*(p+i);

*(p+i)=*(p+j);

*(p+j)=temp;

}

}

}

return 0;

}

(7)字元串c語言編程題擴展閱讀:

printf()函數的調用格式為:printf("&lt;格式化字元串&gt;",&lt;參量表&gt;)。

其中格式化字元串包括兩部分內容:一部分是正常字元,這些字元將按原樣輸出;另一部分是格式化規定字元,以"%"開始,後跟一個或幾個規定字元,用來確定輸出內容格式。

參量表是需要輸出的一系列參數,其個數必須與格式化字元串所說明的輸出參數個數一樣多,各參數之間用","分開,且順序一一對應,否則將會出現意想不到的錯誤。

比如:

int a=1234;

printf("a=%d ",a);

輸出結果為a=1234。

scanf()是C語言中的一個輸入函數。與printf函數一樣,都被聲明在頭文件stdio.h里,因此在使用scanf函數時要加上#include&lt;stdio.h&gt;。

int scanf(const char*restrict format,...);

函數scanf()是從標准輸入流stdin(標准輸入設備,一般指向鍵盤)中讀內容的通用子程序,可以說明的格式讀入多個字元,並保存在對應地址的變數中。

如:

scanf("%d%d",&a,&b);

函數返回值為int型,如果a和b都被成功讀入,那麼scanf的返回值就是2。

⑻ c語言編程題 輸入一字元串,判斷該字元串是否為迴文。

#include <stdio.h>

#include<stdlib.h>

int main()

{

char a[100];

int i=0,j=0;

printf("請輸入字元串: ");

gets(a);

while(a[i]!='')

i++;

i--;

for(;j<=i;i--,j++)

{

if(a[i]!=a[j])

{

break;

}

}

if(j<=i)

{

printf("不是迴文串 ",a);

}

else

{

printf("是迴文串 ",a);

}

system("pause");

return 0;

}

運行效果:

(8)字元串c語言編程題擴展閱讀:

return 0代表程序正常退出。return是C++預定義的語句,它提供了終止函數執行的一種方式。當return語句提供了一個值時,這個值就成為函數的返回值。

return語句用來結束循環,或返回一個函數的值。

1、return 0,說明程序正常退出,返回到主程序繼續往下執行。

2、return 1,說明程序異常退出,返回主調函數來處理,繼續往下執行。return 0或return 1對程序執行的順序沒有影響,只是大家習慣於使用return(0)退出子程序而已。

⑼ C語言編程題 統計數字字元個數

#include<stdio.h>
int main()
{
int i, n = 0;
char s[256];
printf("Please input a string:\n");
scanf("%s",&s);
for (i = 0; s[i]; i++)
if (s[i] >= '0' && s[i] <= '9')
n++;
printf("%d\n", n);
return 0;
}

⑽ C語言編程問題!!!字元串統計

Time
Limit
:
2000/1000ms
(Java/Other)
Memory
Limit
:
65536/32768K
(Java/Other)
Total
Submission(s)
:
113
Accepted
Submission(s)
:
66
Font:
Times
New
Roman
|
Verdana
|Georgia
Font
Size:


Problem
Description
對於給定的一個字元串,統計其中數字字元出現的次數。
Input
輸入數據有多行,第一行是一個整數n,表示測試實例的個數,後面跟著n行,每行包括一個由字母和數字組成的字元串。
Output
對於每個測試實例,輸出該串中數值的個數,每個輸出佔一行。
Sample
Input
2
asdfasdf123123asdfasdf
asdf111111111asdfasdfasdf
Sample
Output
6
9
Author
lcy
Source

閱讀全文

與字元串c語言編程題相關的資料

熱點內容
自己購買雲主伺服器推薦 瀏覽:422
個人所得稅java 瀏覽:761
多餘的伺服器滑道還有什麼用 瀏覽:192
pdf劈開合並 瀏覽:28
不能修改的pdf 瀏覽:752
同城公眾源碼 瀏覽:489
一個伺服器2個埠怎麼映射 瀏覽:298
java字元串ascii碼 瀏覽:79
台灣雲伺服器怎麼租伺服器 瀏覽:475
旅遊手機網站源碼 瀏覽:332
android關聯表 瀏覽:946
安卓導航無聲音怎麼維修 瀏覽:333
app怎麼裝視頻 瀏覽:431
安卓系統下的軟體怎麼移到桌面 瀏覽:96
windows拷貝到linux 瀏覽:772
mdr軟體解壓和別人不一樣 瀏覽:904
單片機串列通信有什麼好處 瀏覽:340
游戲開發程序員書籍 瀏覽:860
pdf中圖片修改 瀏覽:288
匯編編譯後 瀏覽:491