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()方法 查看数组的字节长度 

    例如:

#include<stdio.h>
int main(void)
{
    char a[] = "asdfg";
    int len = sizeof(a);
    printf("数组a的长度为:%d",len);  //数组a的长度为:6 --> "asdfg\0"(实际上字符串以数字0结尾)
    return 0;
}

  4、字符数组的使用

    例一:排序  

#include<stdio.h> 

int main(void)
{
    char a[] = "befacd";
    char i,j,temp;
    for(i=0;i<sizeof(a)-1;i++)
    {
        for(j=1;j<sizeof(a)-1-i;j++)
        {
            if(a[j-1]>a[j])
            {
                temp = a[j-1];
                a[j-1] = a[j];
                a[j] = temp;
            }
        }
     }
    printf("%s\n",a); // 输出: abcdef 

    return 0;
}

     例二:字符串倒置(首尾倒置)

#include<stdio.h>
int main(void)
{
    char str[] = "hello world";
    int i = 0;
    while(str[i++]) ;
    int len = i-1;  //字符串有效长度
    int min = 0;
    int max = len-1; // 下标最大值
    while(min<max)
    {
        char temp = str[min];
        str[min] = str[max];
        str[max] = temp;
        min++;
        max--;
    }
    printf("%s\n",str); // 输出: dlrow olleh
    return 0;
}

    例三:汉字首尾逆置

#include<stdio.h>
int main(void)
{
    char b[] = "你好!明天"; //每个中文字符在gbk编码中占两个字节
    int i = 0;
    while(b[i++]) ; //不断遍历字符串,直至遇到末尾的0,退出
    i--;     // 字符串的有效长度
    int min = 0;
    int max = i-1;
    while(min<max)
    {
        char tmp;
        tmp = b[min];     //调换第一个字节和倒数第二个字符
        b[min] = b[max-1];
        b[max-1] = tmp;

        tmp = b[min+1];   //调换第二个字节和最后一个字符
        b[min+1] = b[max];
        b[max] = tmp;

        min += 2;
        max -= 2;
    }
    printf("倒置后的字符串:%s\n",b);   // 倒置后的字符串:天明!好你

    return 0;
}

    例四:混合统计汉字和ASCII字符串字符  

#include<stdio.h>
int main(void)
{
    char str[] = "厉害了,MyCountry!";
    int len_e = 0;
    int len_c = 0;
    int sum = 0;
    int i,j;
    while(str[i])
    {
        if(str[i]<0)
        {
            len_c += 1;
            i += 2;
         }
         else{
             len_e += 1;
             i += 1;
         }
    }
    sum = len_c+len_e;
    printf("中文字符:%d,英文字符:%d,所有字符总数:%d",len_c,len_e,sum); //中文字符:4,英文字符:10,所有字符总数:14
    return 0;
}

    例五:去除字符串右边的空格

#include<stdio.h>
int main(void)
{
    char c[100] = "hello   ";
    int i = 0;
    int len,j;
    while(c[i++]) ;
    len = i--;
    for(j=len;j>0;j--)
    {
        if(c[j]!=‘ ‘)
        {
            c[j++]=0;
            break;
        }
    }
    printf("取掉末尾的空格后的字符串:%s\n",c); //取掉末尾的空格后的字符串:hello
    return 0;
}

    例六:去除字符串前面的空格

#include<stdio.h>
int main(void)
{
    char s[100] = "    hello,boy";
    int count = 0;  //统计空格长度
    int i;
    while(s[count++]==‘ ‘) //遍历空格
    count--; //取得空格数量
    i = count; //字符开始位置
    while(s[i])
    {
        s[i-count] = s[i]; //第一个字符赋给第一个位置
        i++;
     }
    s[i-count] = 0; //字符串最后赋0
    printf("去除空格后的字符串:%s\n",s);

    return 0;

}

   4、数组总结

    1、数组的本质就是一次定义多个类型相同的变量,同时一个数组中所有的元素在内存中都是顺序存放的。
    2、char s[100] ;s[0]-->s[99],切记没有s[100]这个元素。并且c语言编译器不会帮你检查下标是否有效。
    3、字符串一定是在内存中以0结尾的一个char数组。

    

    

原文地址:https://www.cnblogs.com/schut/p/8546239.html

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

c语言之字符串数组的相关文章

【学习笔记】【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语言的字符串数组使用

一.程序代码例子 #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

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