C语言的字符串数组使用

一、程序代码例子

#include "stdio.h"

main()
{
    int i;
    char s1[5]={‘a‘,‘b‘,‘c‘,‘d‘,‘e‘};//正常打印,但有乱码
    //char s2[5]={‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘\0‘};//报错
    char s3[6]={‘a‘,‘b‘,‘c‘,‘d‘,‘e‘};//正确,末尾自动补零
    char s4[6]={‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘\0‘};//正确
    printf("字符串s1:%s\n",s1);//打印字符串
//    printf("%s\n",s2);
    printf("字符串s3:%s\n",s3);
    printf("字符串s4:%s\n",s4);
    printf("s1字符数组i为5:\n");
    /*for(int i=0;i<5;i++)//错误定义变量i
    {
        printf("%c\n",s1[i]);//打印单个字符
    }*/
    for(i=0;i<5;i++)
    {
        printf("%c\n",s1[i]);//打印单个字符
    }
    printf("s1字符数组i为6:\n");
        for(i=0;i<6;i++)
    {
        printf("%c\n",s1[i]);//打印单个字符
    }
        printf("s4字符数组i为5:\n");
        for(i=0;i<5;i++)
    {
        printf("%c\n",s4[i]);//打印单个字符
    }
        printf("s4字符数组i为6:\n");
        for(i=0;i<6;i++)
    {
        printf("%c\n",s4[i]);//打印单个字符
    }
}

二、实验结果

原文地址:https://www.cnblogs.com/wlei5206/p/12535771.html

时间: 2024-08-03 20:56:28

C语言的字符串数组使用的相关文章

c语言之字符串数组

一.字符串与字符串数组 1.字符数组的定义 char array[100]; 2.字符数组初始化 char array[100] = {'a','b','c'};  //array[0] = 'a'    array[10] = 0 char aray[100] = "abcdef"; char aray[100] = {0}; char aray[] = "qwertyuiop"; //未指定长度时,根据字符串长度自动填写. 3.sizeof()方法 查看数组的字

【学习笔记】【C语言】字符串数组

1.使用场合 * 一维字符数组中存放一个字符串,比如一个名字char name[20] = "mj" * 如果要存储多个字符串,比如一个班所有学生的名字,则需要二维字符数组,char names[15][20]可以存放15个学生的姓名(假设姓名不超过20字符) * 如果要存储两个班的学生姓名,那么可以用三维字符数组char names[2][15][20] 2.初始化 char names[2][10] = { {'J','a','y','\0'}, {'J','i','m','\0'

C语言之字符串数组空格替换

问题描述: 字符串替换空格:请实现一个函数,把字符串中的每个空格替换成"%20".例如输入"we are happy.",则输出"we%20are%20happy.". 代码实现: #include <stdio.h> int replace(char *p) { #if 0  while(*p!='\0')  {   if(*p==' ')   {    printf("%%20");   }   else   

C语言char*字符串数组和unsigned char[]数组的相互转换

#include <iostream> #include <string> using namespace std; void convertUnCharToStr(char* str, unsigned char* UnChar, int ucLen) { int i = 0; for(i = 0; i < ucLen; i++) { //格式化输str,每unsigned char 转换字符占两位置%x写输%X写输 sprintf(str + i * 2, "%

C语言中字符串数组的遍历和比较

/* The list of known types of default packet. */static char  *_default_packet_types[] = {    "ddos client quota",       "ddos server quota",    "blocked ports",           "blocked sites",    "ip scan",    

iOS开发入门 ? C语言(字符串、字符串数组、命令行参数)

字符串 1. 概念 用双引号引起来的就是字符串 "a string" //C语言编译器会将两个并列的字符串自动拼接成一个字符串 "a string""another a string" // \是换行连接的标记(一行不够写) "a looooooooooong \ string" 常见ASCII编码: 'A' == 65    'a' == 97    '0' == 48    '\0' == 0 int a[10]; //表

C语言--二维数组,字符串数组,多维数组

#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { // int a[2][3]={ // {1,2,3}, // {4,5,6} // }; // int a[2][3]={1,2,3,4,5,6}; // //打印单个元素 // printf("%d",a[1][1]); // //元素没赋全,默认为0 // int b[2][3]={{1,2,3},{4}}; // /

黑马程序员-C语言基础:数组和字符串

数组:数组的定义注意点 数组初始化正确写法: int args[5] = {1,23,32,4,5}; int args[5] = {12,23}; int args[5] = {[3]=23, [4]=13};//这种写法也可以,直接给其中角标为3和4的赋值 int args[] = {12,23,32};//中括号中没写数组大小,在大括号中一定要写具体数值 int args['A'] = {2,34,5}; 错误写法: int args[];//这样编译器不知道给你开辟多大的内存空间 int

C语言中字符数组和字符串指针分析

这几天搞Unix上的C程序,里面用到了很多字符数组和字符串指针,我记得在学完C语言后相当一段时间里,对指针这个东西还是模模糊糊,后来工作也没怎么 用到过C,虽然网上这类的文章也有很多,还是决定自己在这做个小总结,也算加深下自己的印象,写了下面的测试程序: #include <stdio.h> int main(int argc, char *argv[]){ char day[15] = "abcdefghijklmn";  char* strTmp = "opq