c语言遍历字符串数组的方法
在这里我们重点介绍遍历字符串的三种方法。
- #include <stdio.h>
- #define MAX_SIZE 1024
- int main()
- {
- char str[MAX_SIZE] = {0};
- int result;
- int i;
- printf("Please input string : ");
- gets(str);
- result = atoi(str);
- printf("result = %d\n",result);
- return 0;
- }
- #include <stdio.h>
- #define MAX_SIZE 1024
- int main()
- {
- char str[MAX_SIZE] = {0};
- int result;
- int i;
- printf("Please input string : ");
- gets(str);
- result = atoi(str);
- printf("result = %d\n",result);
- return 0;
- }
- Please input string : 123456
- result = 123456
- Please input string : 123456
- result = 123456
- Please input string : -123456
- result = -123456
- Please input string : -123456
- result = -123456
- Please input string : 123a456
- result = 123
- Please input string : 123a456
- result = 123
- #include <stdio.h>
- #define MAX_SIZE 1024
- int my_atoi(charchar *str)
- {
- int i = 0;
- int result = 0;
- int flag = 1;
- if (*str == '-')
- {
- flag = -1;
- str++;
- }
- while (*str != '\0')
- {
- if (*str >= '0' && *str <= '9')
- {
- result = result * 10 + ( *str - '0' );
- }
- else
- {
- break;
- }
- str++;
- }
- return result *flag;
- }
- int main()
- {
- char str[MAX_SIZE] = {0};
- int result;
- int i;
- printf("Please input string : ");
- gets(str);
- result = my_atoi(str);
- printf("result = %d\n",result);
- return 0;
- }
- #include <stdio.h>
- #define MAX_SIZE 1024
- int my_atoi(charchar *str)
- {
- int i = 0;
- int result = 0;
- int flag = 1;
- if (*str == '-')
- {
- flag = -1;
- str++;
- }
- while (*str != '\0')
- {
- if (*str >= '0' && *str <= '9')
- {
- result = result * 10 + ( *str - '0' );
- }
- else
- {
- break;
- }
- str++;
- }
- return result *flag;
- }
- int main()
- {
- char str[MAX_SIZE] = {0};
- int result;
- int i;
- printf("Please input string : ");
- gets(str);
- result = my_atoi(str);
- printf("result = %d\n",result);
- return 0;
- }
- Please input string : 987654321
- result = 987654321
- Please input string : 987654321
- result = 987654321
- Please input string : -123456789
- result = -123456789
- Please input string : -123456789
- result = -123456789
- Please input string : 123456a789
- result = 123456
- Please input string : 123456a789
- result = 123456
- #include <stdio.h>
- #include <string.h>
- #define MAX_SIZE 1024
- int main()
- {
- char src[MAX_SIZE] = {0};
- int i;
- int len;
- printf("Please input string : ");
- gets(src);
- len = strlen(src);
- printf("string = ");
- for (i = 0; i < len; i++)
- {
- printf("%c",src[i]);
- }
- printf("\n");
- return 0;
- }
- #include <stdio.h>
- #include <string.h>
- #define MAX_SIZE 1024
- int main()
- {
- char src[MAX_SIZE] = {0};
- int i;
- int len;
- printf("Please input string : ");
- gets(src);
- len = strlen(src);
- printf("string = ");
- for (i = 0; i < len; i++)
- {
- printf("%c",src[i]);
- }
- printf("\n");
- return 0;
- }
- Please input string : abcdefg123456
- string = abcdefg123456
- Please input string : abcdefg123456
- string = abcdefg123456
- #include <stdio.h>
- #include <string.h>
- #define MAX_SIZE 1024
- int main()
- {
- char src[MAX_SIZE] = {0};
- int i = 0;
- printf("Please input string : ");
- gets(src);
- printf("string = ");
- while (src[i] != '\0')
- {
- printf("%c",src[i]);
- i++;
- }
- printf("\n");
- return 0;
- }
- #include <stdio.h>
- #include <string.h>
- #define MAX_SIZE 1024
- int main()
- {
- char src[MAX_SIZE] = {0};
- int i = 0;
- printf("Please input string : ");
- gets(src);
- printf("string = ");
- while (src[i] != '\0')
- {
- printf("%c",src[i]);
- i++;
- }
- printf("\n");
- return 0;
- }
- Please input string : congcong123456
- string = congcong123456
- Please input string : congcong123456
- string = congcong123456
- #include <stdio.h>
- #include <string.h>
- #define MAX_SIZE 1024
- int main()
- {
- char src[MAX_SIZE] = {0};
- charchar *temp = src;
- printf("Please input string : ");
- gets(src);
- printf("string = ");
- while (*temp != '\0')
- {
- printf("%c",*temp);
- temp++;
- }
- printf("\n");
- return 0;
- }
- #include <stdio.h>
- #include <string.h>
- #define MAX_SIZE 1024
- int main()
- {
- char src[MAX_SIZE] = {0};
- charchar *temp = src;
- printf("Please input string : ");
- gets(src);
- printf("string = ");
- while (*temp != '\0')
- {
- printf("%c",*temp);
- temp++;
- }
- printf("\n");
- return 0;
- }
- Please input string : congcong123
- string = congcong123
- Please input string : congcong123
- string = congcong123
转载于:https://www.cnblogs.com/i6010/articles/7908194.html
c语言遍历字符串数组的方法相关推荐
- C语言 遍历字符串数组
遍历字符串数组 #include <stdio.h>int main(void){int i;char cs[][6] = {"VV", "cat" ...
- 【C语言】字符串数组按字典升序
[C语言]字符串数组按字典升序 文章目录 [C语言]字符串数组按字典升序 一.使用strcpy深拷贝实现字符串交换 二.交换字符指针数组中的指针位置,实现字符串交换 在使用C语言操作字符串时,容易出现 ...
- c语言中字符串数组的地址存放以及%s输出单个字符导致程序崩溃的问题
代码 总结下c语言中字符串数组的地址存放问题 #include <iostream> using namespace std; #include<bits/stdc++.h>i ...
- Go语言中字符串的查找方法小结
这篇文章主要介绍了Go语言中字符串的查找方法小结,示例的main函数都是导入strings包然后使用其中的方法,需要的朋友可以参考下 1.func Contains(s, substr string) ...
- linux c之遍历字符串数组
1 问题 比如我们要遍历字符串数组,我们的思路一般是先求字符串数组的长度,然后再用for循环便利,其实没必要这样,我们直接在 字符串数组后面加上个NULL就行再去遍历 2 代码实现 #include ...
- 【C语言】字符数组初始化方法
目录 1.字符串数组初始化方法 2.字符串表达格式 1.字符串数组初始化方法 #include <stdio.h>int main() {// 字符串初始化方法 默认元素个数// 默认元素 ...
- C语言计算字符串长度的方法
C语言计算字符串长度的方法 思路分析 字符串的结束标志是'\0',因此计算字符串的长度的核心思想就是通过字符指针顺序检索每一个字符,直到检测到'\0'为止,以下是实现该算法的几种方式. 代码实现 1. ...
- 输入字符串和遍历字符数组的方法
输入字符串: 1. gets(a) //首选,对字符无要求,但是需要加上头文件#include <string.h> 2.scanf("%s",a) //不可输入空格, ...
- c语言字符串分割存放到数组,用于把一个字符串分割成字符串数组的方法是?()...
函数readDat()是从文件in71.dat中读取20行数据存放到字符串数组xx中(每行字符串长度均小于80).请 函数readDat()是从文件in71.dat中读取20行数据存放到字符串数组xx ...
最新文章
- Linux在shell终端中清空DNS缓存,刷新DNS的方法(ubuntu,debian)
- SAP收购Sybase意欲何为
- Linux下MySql插入汉字报错解决(/etc/my.cnf不存在)
- 福建师范大学计算机考研好考吗,福建师范大学考研难吗?一般要什么水平才可以进入?...
- sqoop简介与安装配置
- Linux配置编程环境+云服务器上传文件
- 基于HAProxy+Keepalived高可用负载均衡web服务的搭建
- c++网络编程连接成功后回调onconnected_谈谈网络编程(基于C++)
- 他帮 10 多家公司变身独角兽,总结出一份“成功”清单
- C语言之生成汇编代码(十)
- 前大嗅万万没想到系列之520奇葩礼物大盘点,活着不好吗?
- 南大网院计算机基础第一次作业,南大网院2015计算机基础第一次作业.docx
- 软件项目管理工具简介
- 《计算机组网试验-DNS域名服务协议 》杭州电子科技大学
- Flink中水位线/Periodic周期水印/Punctuated每个事件水印实现原理/ PunctuatedWatermarks/PeriodicWatermarks
- 橙旗贷受邀参加浦东企联举行的迎新年书法笔会
- Kesci--基于机器学习的故障检测系统
- 10分钟内在windows下安装woocommerce开发测试环境
- 软件开发2:代码检视
- php prepare错误,php环境错误,Loader.php报错