c语言字符串输出一部分

推荐 科技 2019-01-22 14:46:16 3419

本文收集整理关于c语言字符串输出一部分的相关议题,使用内容导航快速到达。

内容导航:

  • Q1:c语言输出字符串数组出现乱码
  • Q2:C语言:字符串输入与输出相关问题
  • Q3:C语言中怎么输出一个字符串空格后面的部分
  • Q4:在c语言中怎么输出一个字符串的长度是多少
  • Q5:c语言中如何借用指针输出字符串?
  • Q6:C语言 输入一个字符串 要求输出的字符是原来字符的下一个字符
  • Q7:C语言只输出一段字符串
  • Q1:c语言输出字符串数组出现乱码

    字符串数组各字符单个赋值的话,你得主动在每串最后一个字符之后添加 \0,表示串结束了,输入时就没有多余的乱码了,因为gets()或scanf() 的%s都是主动在串结束后添加了 \0的

    Q2:C语言:字符串输入与输出相关问题

    getchar和putchar都是对字符操作的,而不是字符串,所以需要设计循环为字符串中每一个字符赋值。

    以下是示例代码,需要说明的一点是gets输入字符串对于字符串长度没有限制,可能导致越界溢出,不安全,建议改用fgets,另外在新的微软标准中gets函数已被gets_s函数代替,希望对你有帮助。

    includeintmain(){ const int count=21;

    charstr1[count];charstr2[count];charch;printf("请str1输入字符串(getchar方式):\n");int i=0;while((ch=getchar())!\n){ str1[i]=ch;i+;if(i=count-1){ str1[count-1]=\0;break;} }

    str1[i+]=\0;printf("下面输出str1(putchar方式):\n");int j=0;while(str1[j]!\0){putchar(str1[j]);j+;}/printf

    ("请str1输入字符串(gets方式):\n");gets(str2);printf("下面输出str1(puts方式):\n");puts(str2);return0;}

    1.#include"stdio.hmain(){charc;int letters=0,space=0,digit=0,others=0;printf("pleaseinputsome characters\n");while((c=getchar())!\n){

    if(c>=a&c|c>=A&c)

    letters+;else if(c= )space+;else if(c>=0&c)

    digit+;else

    others+;}printf("all in all:char=dspace=d digit=d others=d\n",letters,space,digit,others);}

    2.#include"stdio.hmain()

    {

    int a,n,count=1;long int sn=0,tn=0;printf("pleaseinputa and n\n");scanf("%d,%d",&a,&n);printf("a=d,n=d\n",a,n);while(count)

    {

    tn=tn+a;sn=sn+tn;a=a*10;count;}printf("a+aa+.=ld\n",sn);}

    3.#include"stdio.hmain()

    {

    int a[11]={1,4,6,9,13,16,19,28,40,100};inttemp1,temp2,number,end,i,j;printf("originalarrayis:\n");for(i=0;i;i+)printf("%5d",a[i]);printf("\n");printf("inserta newnumber:");scanf("%d",&number);end=a[9];if(number>end)

    a[10]=number;else

    {for(i=0;i;i+){ if(a[i]>number){temp1=a[i];a[i]=number;for(j=i+1;j;j+){temp2=a[j];a[j]=temp1;temp1=temp2;}

    break;}

    }

    }

    for(i=0;i;i+)printf("%6d",a[i]);}

    Q3:C语言中怎么输出一个字符串空格后面的部分

    c语言中输出字符串的函数有printf("%s") puts() fputs()等,字符串中可以是任意的字符,包括空格在内,无特殊处理,如:
    char str[]="hello world" ;
    printf("%s\n", str );
    puts(str);
    fputs(str,stdin);
    但在输入带有空格的字符串时,只能用gets()或fgets(),而不能用scanf("%s") ,因为scanf("%s")输入字符串时,遇到空格就结束了输入。而gets()函数是以回车为结束符的输入函数,可以输入带空格的字符串。

    Q4:在c语言中怎么输出一个字符串的长度是多少

    123charstr[]="123";strlen(str)这个就求出长度了然后printf("len[%d]",strlen(str));就输出了这个字符串的长度

    Q5:c语言中如何借用指针输出字符串?

    使用指针输出字符串有以下几种方式:

    1、使用printf 函数进行输出,其使用的格式转换符为%s,如

    char*str="test";//指针指向一个字符串

    printf("%s\n",str);//输出str指向的字符串

    2、使用puts函数进行输出,如

    char*str="test";

    puts(str);//输出str指向的字符串,会自动多输出一个换行

    3、使用自定义函数进行输出,如

    voidmyPuts(char*str)//自定义输出函数

    {

    if(!str)return;

    while(*str!=\0){

    putchar(*str);

    str++;

    }

    }

    char*str="test";

    myPuts(str);

    扩展资料:

    C++指针与字符串

    1、C语言里没有字符串数据类型,要想获得字符串的表示形式利用字符数组

    #include

    using namespace std;

    #include

    void main()

    {

    char ar[]={a,b,c,d};

    cout<

    cout<

    char br[]={a,b,c,d,\0};

    cout<

    cout<

    char cr[5]="abcd"; //字符串结尾默认隐藏了\0

    cout<

    system("PAUSE");

    }

    2、C语言里没有字符串数据类型,要想获得字符串的表示形式利用字符指针

    #include

    using namespace std;

    #include

    void main()

    {

    char *p="hello world";

    cout<

    //整形的指针,打印指针时只能打印其内部地址

    //字符指针,打印指针时也是地址,但是这个被看作字符指针后,会打印该指针指向地址内存放的字符串,打印直到遇到\0为止

    system("PAUSE");

    }

    3、静态常量区的字符串存储及指针访问

    #include

    using namespace std;

    #include

    #include

    void main()

    {

    char *p="hello world"; //hello world存放在内存的静态常量区

    //指针变量p存储的是该静态常量区的首个字符地址

    //不能通过指针修改静态常量区的字符,但是可以通过指针访问

    int length=strlen(p);

    //strlen计算的是字符串p的有效长度,不算\0

    for(int i=0;i

    {

    cout<

    }

    system("PAUSE");

    }

    Q6:C语言 输入一个字符串 要求输出的字符是原来字符的下一个字符

    #include
    int main(){
    char s[256],i,L;
    printf("input one line string:\n");
    //scanf("%s",s);用于输入一个字符串,到空白为止
    //gets(s); 用于输入一行字符串,含空格,不含回车换行,不安全方式
    fgets(s,255,stdin); //用于输入一行字符串,含空格,含回车换行,安全方式
    L = strlen(s);//字符串长度
    for (i=0;iprintf("%s\n",s);
    return 0;
    }

    Q7:C语言只输出一段字符串

    这个,直接打印第10-50个元素不就可以了吗?
    int i;
    for(i = 9;i < 50; i++)
    {
    printf("str[%d] = %c\t",i,str[i]);
    }

    相关文章
    c语言怎么生成exe文件2019-01-27
    c语言十进制转十六进制2019-02-13
    c语言定义多个结构体2019-02-17
    c语言输入数字变成字母2019-02-19
    设置c语言中字体颜色2019-02-20
    c语言逻辑运算符优先级2019-02-21
    C语言最大值最小值编程2019-02-22
    c语言打开文件怎么输入2019-02-25
    c语言 数组和指针区别2019-02-26
    c语言字符串长度怎么算2019-02-27