A. c语言输入输出字符串
ch【5】缓冲区过短,导致溢出,ch【5】中只存了 你输入的前5个字符,限定输入用scanf(“%5s”,ch)就行了,这样ch中就是就是存的你数入的前5个字符,至于char ch;错误是因为格式化错误,即%s和ch不匹配,char ch【】在c语言中是不成立的,数组必须是定长的。
B. c语言中怎么输出字符串中的某个字符
错误在于你判断了第一个非@字符时就已经输出没有字符@退出循环了所以不会检测@了。改成下面就行了:
#include
#include
int
main()
{
char
sh[100],n=0;
gets(sh);
for(int
i=0;sh[i];i
)
if(sh[i]=='@')
n
;
if(n==0)
printf("没有字符
@\n");
else
printf("有字符
@\n");
}
C. 用C语言文件如何输出指定字符串
只说思路,具体函数用法请自行解决
fopen打开文件
循环(for)读入每行(fgets)
判断该行是否以X开头,是则输出前六个字符,否不输出
D. C语言将输入中包含特定“模式”或字符串的各行打印出来
这题就是让你比较两个字符串,判断一个字符串存在于另一个字符串中。
以题目举例,将字符串“ould”分别和4行字符串比较,只要前者存在于后者,就打印该行字符串。
我下面的代码,多行字符串用二维数组str1保存,模式字符串用数组str2保存。我就不手动输入了,你需要自己加个scanf给str1和str2赋值。
#include<stdio.h>
#include<string.h>
#define M 4//最大输入行数
int include(char *str1,char *str2);//检查str2存在于str1中则返回1,否则返回0
int main()
{
int i;
char str1[M][100]={"Ah Love could you....","To grasp this sorry Scheme...","Would not we shatter it to...","Re-mould it nearer to the..."};
char str2[50]="ould";
for(i=0;i<M;i++)
if(include(str1[i],str2))printf("%s ",str1[i]);
return 0;
}
int include(char *str1,char *str2)
{
int len1,len2,i,j,re=0;
len1=strlen(str1);
len2=strlen(str2);
for(i=0;i<len1;i++)
{
for(j=0;j<len2;j++)
if(str2[j]!=str1[i+j])
break;
if(j==len2){re=1;break;}
}
return re;
}
E. 怎样在C语言中实现输入几个字符串后按指定格式输出这几个字符串
#include<stdio.h>
int main()
{
char a[20];//字符数组
char b[20];
char c[20];
scanf("%s %s %s", a, b, c);//输入三个字符串中间用空格隔开如: aaaa bbbb cccc
printf("%s\n%s\t%s\n",a, b, c);
return 0;
}
运行下,看看这个是不是你要的意思?
F. C语言上中怎么样直接输入输出一个字符串
C语言中字符串的输入和输出主要有两种方式:
输入使用:scanf("%s",a);对应的输出使用:printf("%s ",a);
输入使用:gets(b);对应的输出使用:puts(b);
参考代码:
#include<stdio.h>
intmain()
{
chara[100],b[100];
scanf("%s",a);//方法一
printf("%s ",a);
getchar();//吸收空格
gets(b);//方法二
puts(b);
return0;
}
/*
运行结果:
123
123
456
456
*/
G. c语言中如何输入输出字符串
在<string.h>头文件中用字符串处理函数输入和输出,gest用来输入,puts用来输出。
字符串输入函数gets,从键盘键入以回车结束的字符串放入字符数组中,并自动加’ '。输入串长度应小于字符数组维数,字符串中可以包含空格。
字符串输出函数puts,向显示器输出字符串(输出完,自动换行,即用' '替'