导航:首页 > 编程语言 > 字符串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语言编程题相关的资料

热点内容
圆命令画法 浏览:303
如果给电脑e盘文件加密 浏览:799
javaswing项目 浏览:774
androidsdksetup 浏览:1001
pdf怎么设置中文 浏览:124
安卓手机用什么软件看伦敦金 浏览:962
魅族文件夹无名称 浏览:787
苏黎世无人机算法 浏览:872
核桃编程和小码王的融资 浏览:681
微积分教材pdf 浏览:723
写python给微信好友发消息 浏览:336
蚊帐自营米加密 浏览:418
学校推荐核桃编程 浏览:802
湖南农信app怎么导明细 浏览:471
福特abs编程 浏览:506
如何自学安卓手机 浏览:437
以太坊源码共识机制 浏览:910
单片机探测器 浏览:870
demo编程大赛作品怎么运行 浏览:51
学历提升用什么手机软件App 浏览:938