一、字符串与字符串数组

  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 ;
}

  4、字符数组的使用

    例一:排序  

#include<stdio.h> 

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

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

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

    例三:汉字首尾逆置

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

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

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

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

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

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

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

   4、数组总结

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

    

    

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

  1. c语言中字符串数组初始化的一点总结&& c++访问控制的三种方式

    char *c[]={"ONE","TWO","THREE","FOUR"}; // c语言中定义了一个字符串数组(也称 ...

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

    1.使用场合 * 一维字符数组中存放一个字符串,比如一个名字char name[20] = "mj" * 如果要存储多个字符串,比如一个班所有学生的名字,则需要二维字符数组,cha ...

  3. C语言计算字符串数组中每个字符串出现的个数

    unsigned int str_num(char *str[], int num[], int len) { int i, j; int count; int flag[len]; ; i < ...

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

    #include <iostream> #include <string> using namespace std; void convertUnCharToStr(char* ...

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

    /* The list of known types of default packet. */static char  *_default_packet_types[] = {    "d ...

  6. C语言学习018:strdup复制字符串数组

    在C语言学习005:不能修改的字符串中我们知道字符串是存储在常量区域的,将它赋值给数组实际是将常量区的字符串副本拷贝到栈内存中,如果将这个数组赋值给指针,我们可以改变数组中的元素,就像下面那样 int ...

  7. C语言基础:数组和字符串

    数组:数组的定义注意点 数组初始化正确写法: int args[5] = {1,23,32,4,5}; int args[5] = {12,23}; int args[5] = {[3]=23, [4 ...

  8. C语言数组:C语言数组定义、二维数组、动态数组、字符串数组

    1.C语言数组的概念 在<更加优美的C语言输出>一节中我们举了一个例子,是输出一个 4×4 的整数矩阵,代码如下: #include <stdio.h> #include &l ...

  9. C语言基础知识-数组和字符串

    C语言基础知识-数组和字符串 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数组概述 在程序设计中,为了方便处理数据把具有相同类型的若干变量按有序形式组织起来的方式我们称为数组 ...

随机推荐

  1. SVN 的搭建及使用(二)VisualSVN Server建立版本库,以及VisualSVN和TortoiseSVN的使用

    上一篇介绍了VisualSVN Server和TortoiseSVN的下载,安装,汉化.这篇介绍一下如何使用VisualSVN Server建立版本库,以及VisualSVN和TortoiseSVN的 ...

  2. 理解 EventLoop

    链接 链接 node 浏览器 执行顺序有差异 macrotask microtask 一个线程会有 堆 栈 消息队列;  栈函数执行是用的, 堆用了存放定义的对象, 消息队列来处理异步的操作 a() ...

  3. [置顶] Android App引导页这些坑你自己犯过吗?

    场景:测试机:华为荣耀6x 今天我自己掉入一个很蠢蠢的坑,一个引导页搞了20多分钟,不管我怎么测试用真机还是模拟器都无法运行,但是我写的demo完全没问题,好无语,我都怀疑我是不是搞android,我 ...

  4. So sad! ,Asphyre Closure

    So sad ! Asphyre Closure! 不过我相信开发App用Delphi应该是快的. 越与平台无关,优势越明显. 积累的组件市场. 虽然objc 有很多开源支持. 但总感觉还不够delp ...

  5. Tomcat启动超时问题Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds

    使用Eclipse启动Tomcat时出现启动超时的问题如下所示: Server Tomcat v7.0 Server at localhost was unable to start within 4 ...

  6. 一种 jquery 检索方案

    整理自:http://www.cnblogs.com/linjiqin/archive/2011/03/18/1988464.html <!DOCTYPE HTML PUBLIC "- ...

  7. 剑指offer-第五章优化时间和空间效率(数组中出现次数超过一半的数字)

    题目:输入一个数组,找出一个数字,它在数组中出现的次数超过数组的一半. 题目规定如果可以改变数组中元素的位置. 思路1:如果数组是排序的,那么中间元素的位置不就是次数超过数组一半的元素吗?是的,因此我 ...

  8. C# null和" "的区别

    String str1 = null;   str引用为空 String str2 = "";      str引用一个空串 也就是null没有分配空间,""分 ...

  9. CentOS 6+Hadoop 2.6.0分布式集群安装

    1.角色分配 IP Role Hostname 192.168.18.37 Master/NameNode/JobTracker HDP1 192.168.18.35 Slave/DataNode/T ...

  10. CF311B Cats Transport

    题意 Zxr960115 is owner of a large farm. He feeds m cute cats and employs p feeders. There's a straigh ...