在这里我们重点介绍遍历字符串的三种方法。

首先我们来看一道编程题目:
输入一个字符串,且都是数字,也可以是负数,转化成相应的整型数并输出,若输入字母则停止。
我们知道,在C语言里有一个函数是“atoi”,它可以把字符串转换成整型数,包含在头文件stdlib.h中。以下是我们使用了这个函数的代码。
[objc] view plain copy
  1. #include <stdio.h>
  2. #define MAX_SIZE 1024
  3. int main()
  4. {
  5. char str[MAX_SIZE] = {0};
  6. int result;
  7. int i;
  8. printf("Please input string : ");
  9. gets(str);
  10. result = atoi(str);
  11. printf("result = %d\n",result);
  12. return 0;
  13. }
[objc] view plain copy
  1. #include <stdio.h>
  2. #define MAX_SIZE 1024
  3. int main()
  4. {
  5. char str[MAX_SIZE] = {0};
  6. int result;
  7. int i;
  8. printf("Please input string : ");
  9. gets(str);
  10. result = atoi(str);
  11. printf("result = %d\n",result);
  12. return 0;
  13. }
运行结果:
正数:
[objc] view plain copy
  1. Please input string : 123456
  2. result = 123456
[objc] view plain copy
  1. Please input string : 123456
  2. result = 123456

负数:
[objc] view plain copy
  1. Please input string : -123456
  2. result = -123456
[objc] view plain copy
  1. Please input string : -123456
  2. result = -123456
带字母的字符串:
[objc] view plain copy
  1. Please input string : 123a456
  2. result = 123
[objc] view plain copy
  1. Please input string : 123a456
  2. result = 123

使用“atoi”函数做这道题很简单,那么我们能不能自己写一个函数来实现把字符串转换成整型数的功能呢?下面是我们自己写一个函数来实现把字符串转换成整型数的功能的代码。
[objc] view plain copy
  1. #include <stdio.h>
  2. #define MAX_SIZE 1024
  3. int my_atoi(charchar *str)
  4. {
  5. int i = 0;
  6. int result = 0;
  7. int flag = 1;
  8. if (*str == '-')
  9. {
  10. flag = -1;
  11. str++;
  12. }
  13. while (*str != '\0')
  14. {
  15. if (*str >= '0' && *str <= '9')
  16. {
  17. result = result * 10 + ( *str - '0' );
  18. }
  19. else
  20. {
  21. break;
  22. }
  23. str++;
  24. }
  25. return result *flag;
  26. }
  27. int main()
  28. {
  29. char str[MAX_SIZE] = {0};
  30. int result;
  31. int i;
  32. printf("Please input string : ");
  33. gets(str);
  34. result = my_atoi(str);
  35. printf("result = %d\n",result);
  36. return 0;
  37. }
[objc] view plain copy
  1. #include <stdio.h>
  2. #define MAX_SIZE 1024
  3. int my_atoi(charchar *str)
  4. {
  5. int i = 0;
  6. int result = 0;
  7. int flag = 1;
  8. if (*str == '-')
  9. {
  10. flag = -1;
  11. str++;
  12. }
  13. while (*str != '\0')
  14. {
  15. if (*str >= '0' && *str <= '9')
  16. {
  17. result = result * 10 + ( *str - '0' );
  18. }
  19. else
  20. {
  21. break;
  22. }
  23. str++;
  24. }
  25. return result *flag;
  26. }
  27. int main()
  28. {
  29. char str[MAX_SIZE] = {0};
  30. int result;
  31. int i;
  32. printf("Please input string : ");
  33. gets(str);
  34. result = my_atoi(str);
  35. printf("result = %d\n",result);
  36. return 0;
  37. }

运行结果:
正数:
[objc] view plain copy
  1. Please input string : 987654321
  2. result = 987654321
[objc] view plain copy
  1. Please input string : 987654321
  2. result = 987654321

负数:
[objc] view plain copy
  1. Please input string : -123456789
  2. result = -123456789
[objc] view plain copy
  1. Please input string : -123456789
  2. result = -123456789

带字母:
[objc] view plain copy
  1. Please input string : 123456a789
  2. result = 123456
[objc] view plain copy
  1. Please input string : 123456a789
  2. result = 123456

我们可以看到,用我们自己编写的函数运行的结果也是正确的。那么我们该怎么样编写这个函数呢?其实这里主要的知识点是字符串的遍历问题。如果我们想把字符串转化成整型数,那么我们需要一个一个地访问字符串里的内容,即字符串遍历。
首先我们介绍遍历字符串的三种方法:
1. for循环(字符数组)
[objc] view plain copy
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAX_SIZE 1024
  4. int main()
  5. {
  6. char src[MAX_SIZE] = {0};
  7. int i;
  8. int len;
  9. printf("Please input string : ");
  10. gets(src);
  11. len = strlen(src);
  12. printf("string = ");
  13. for (i = 0; i < len; i++)
  14. {
  15. printf("%c",src[i]);
  16. }
  17. printf("\n");
  18. return 0;
  19. }
[objc] view plain copy
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAX_SIZE 1024
  4. int main()
  5. {
  6. char src[MAX_SIZE] = {0};
  7. int i;
  8. int len;
  9. printf("Please input string : ");
  10. gets(src);
  11. len = strlen(src);
  12. printf("string = ");
  13. for (i = 0; i < len; i++)
  14. {
  15. printf("%c",src[i]);
  16. }
  17. printf("\n");
  18. return 0;
  19. }
运行结果:
[objc] view plain copy
  1. Please input string : abcdefg123456
  2. string = abcdefg123456
[objc] view plain copy
  1. Please input string : abcdefg123456
  2. string = abcdefg123456
在这里我们首先利用了strlen函数测量字符数组的长度,然后用for循环遍历字符串,将输入的字符串的内容一个字符一个字符输出。
2. while循环(字符数组)
[objc] view plain copy
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAX_SIZE 1024
  4. int main()
  5. {
  6. char src[MAX_SIZE] = {0};
  7. int i = 0;
  8. printf("Please input string : ");
  9. gets(src);
  10. printf("string = ");
  11. while (src[i] != '\0')
  12. {
  13. printf("%c",src[i]);
  14. i++;
  15. }
  16. printf("\n");
  17. return 0;
  18. }
[objc] view plain copy
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAX_SIZE 1024
  4. int main()
  5. {
  6. char src[MAX_SIZE] = {0};
  7. int i = 0;
  8. printf("Please input string : ");
  9. gets(src);
  10. printf("string = ");
  11. while (src[i] != '\0')
  12. {
  13. printf("%c",src[i]);
  14. i++;
  15. }
  16. printf("\n");
  17. return 0;
  18. }
运行结果:
[objc] view plain copy
  1. Please input string : congcong123456
  2. string = congcong123456
[objc] view plain copy
  1. Please input string : congcong123456
  2. string = congcong123456

由于输入的字符串的长度是未知的,然而我们遍历字符串的时候需要用到循环,我们知道当循环次数未知时,最好使用while语句。
3.while循环(指针)
[objc] view plain copy
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAX_SIZE 1024
  4. int main()
  5. {
  6. char src[MAX_SIZE] = {0};
  7. charchar *temp = src;
  8. printf("Please input string : ");
  9. gets(src);
  10. printf("string = ");
  11. while (*temp != '\0')
  12. {
  13. printf("%c",*temp);
  14. temp++;
  15. }
  16. printf("\n");
  17. return 0;
  18. }
[objc] view plain copy
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAX_SIZE 1024
  4. int main()
  5. {
  6. char src[MAX_SIZE] = {0};
  7. charchar *temp = src;
  8. printf("Please input string : ");
  9. gets(src);
  10. printf("string = ");
  11. while (*temp != '\0')
  12. {
  13. printf("%c",*temp);
  14. temp++;
  15. }
  16. printf("\n");
  17. return 0;
  18. }
运行结果:
[objc] view plain copy
  1. Please input string : congcong123
  2. string = congcong123
[objc] view plain copy
  1. Please input string : congcong123
  2. string = congcong123

在这里我们首先定义了一个指针变量,指向数组的首地址,那为什么要定义这个指针变量呢?为什么不直接用“src++;”呢?
首先,我们要知道的是数组名代表了什么:
①指针常量
②数组首元素的地址
既然数组名代表了指针常量,常量怎么可以自增呢?所以不可以用“src++;”,如果使用“src++;”,那么在编译时便会报错“错误:自增运算中的左值无效”。
以上为遍历字符串的三种方法,希望我们以后可以熟练地运用这三种方法遍历字符串。
在上述“将字符串转化成整型数”的编程题中,还有一个小知识点,就是如何准确地将正数和负数表示出来。首先我们可以利用一个“flag”,我们将flag初始化为1,符号会出现在我们所输入的字符串的首位,只需要判断这个是不是‘-’,如果是的话,将flag置为-1,最后将结果与flag相乘即可,如果是正数,则不用管,正数乘1还是原数。
转载:http://blog.csdn.net/nopoppy/article/details/52613975

转载于:https://www.cnblogs.com/i6010/articles/7908194.html

c语言遍历字符串数组的方法相关推荐

  1. C语言 遍历字符串数组

    遍历字符串数组 #include <stdio.h>int main(void){int i;char cs[][6] = {"VV", "cat" ...

  2. 【C语言】字符串数组按字典升序

    [C语言]字符串数组按字典升序 文章目录 [C语言]字符串数组按字典升序 一.使用strcpy深拷贝实现字符串交换 二.交换字符指针数组中的指针位置,实现字符串交换 在使用C语言操作字符串时,容易出现 ...

  3. c语言中字符串数组的地址存放以及%s输出单个字符导致程序崩溃的问题

    代码 总结下c语言中字符串数组的地址存放问题 #include <iostream> using namespace std; #include<bits/stdc++.h>i ...

  4. Go语言中字符串的查找方法小结

    这篇文章主要介绍了Go语言中字符串的查找方法小结,示例的main函数都是导入strings包然后使用其中的方法,需要的朋友可以参考下 1.func Contains(s, substr string) ...

  5. linux c之遍历字符串数组

    1 问题 比如我们要遍历字符串数组,我们的思路一般是先求字符串数组的长度,然后再用for循环便利,其实没必要这样,我们直接在 字符串数组后面加上个NULL就行再去遍历 2 代码实现 #include ...

  6. 【C语言】字符数组初始化方法

    目录 1.字符串数组初始化方法 2.字符串表达格式 1.字符串数组初始化方法 #include <stdio.h>int main() {// 字符串初始化方法 默认元素个数// 默认元素 ...

  7. C语言计算字符串长度的方法

    C语言计算字符串长度的方法 思路分析 字符串的结束标志是'\0',因此计算字符串的长度的核心思想就是通过字符指针顺序检索每一个字符,直到检测到'\0'为止,以下是实现该算法的几种方式. 代码实现 1. ...

  8. 输入字符串和遍历字符数组的方法

    输入字符串: 1. gets(a) //首选,对字符无要求,但是需要加上头文件#include <string.h> 2.scanf("%s",a) //不可输入空格, ...

  9. c语言字符串分割存放到数组,用于把一个字符串分割成字符串数组的方法是?()...

    函数readDat()是从文件in71.dat中读取20行数据存放到字符串数组xx中(每行字符串长度均小于80).请 函数readDat()是从文件in71.dat中读取20行数据存放到字符串数组xx ...

最新文章

  1. Linux在shell终端中清空DNS缓存,刷新DNS的方法(ubuntu,debian)
  2. SAP收购Sybase意欲何为
  3. Linux下MySql插入汉字报错解决(/etc/my.cnf不存在)
  4. 福建师范大学计算机考研好考吗,福建师范大学考研难吗?一般要什么水平才可以进入?...
  5. sqoop简介与安装配置
  6. Linux配置编程环境+云服务器上传文件
  7. 基于HAProxy+Keepalived高可用负载均衡web服务的搭建
  8. c++网络编程连接成功后回调onconnected_谈谈网络编程(基于C++)
  9. 他帮 10 多家公司变身独角兽,总结出一份“成功”清单
  10. C语言之生成汇编代码(十)
  11. 前大嗅万万没想到系列之520奇葩礼物大盘点,活着不好吗?
  12. 南大网院计算机基础第一次作业,南大网院2015计算机基础第一次作业.docx
  13. 软件项目管理工具简介
  14. 《计算机组网试验-DNS域名服务协议 》杭州电子科技大学
  15. Flink中水位线/Periodic周期水印/Punctuated每个事件水印实现原理/ PunctuatedWatermarks/PeriodicWatermarks
  16. 橙旗贷受邀参加浦东企联举行的迎新年书法笔会
  17. Kesci--基于机器学习的故障检测系统
  18. 10分钟内在windows下安装woocommerce开发测试环境
  19. 软件开发2:代码检视
  20. php prepare错误,php环境错误,Loader.php报错

热门文章

  1. 说说“偏差处理”那点事
  2. 「走过」微软、优步,老工程师告诉你哪些数据结构和算法最重要
  3. SAP MM 标准的采购订单预付款功能介绍
  4. 容量是GPT-2的1.7倍!谷歌打造神经对话模型Meena
  5. 澎思科技马原:AI安防竞争还未结束,落地进入后发优势时代
  6. 聚类分析:创建,可视化以及可解释性
  7. 「每周CV论文推荐」 初学深度学习人脸识别和验证必读文章
  8. 新兴AI解决方案将越来越依赖于嵌入式视觉技术
  9. ICLR 2019计算机视觉、NLP、图模型、对抗学习、表示学习和元学习
  10. SAP 同一个序列号可以同时出现在2个不同的HU里?