在这里我们重点介绍遍历字符串的三种方法。
首先我们来看一道编程题目:
输入一个字符串,且都是数字,也可以是负数,转化成相应的整型数并输出,若输入字母则停止。
我们知道,在C语言里有一个函数是“atoi”,它可以把字符串转换成整型数,包含在头文件stdlib.h中。以下是我们使用了这个函数的代码。
- #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
带字母的字符串:
使用“atoi”函数做这道题很简单,那么我们能不能自己写一个函数来实现把字符串转换成整型数的功能呢?下面是我们自己写一个函数来实现把字符串转换成整型数的功能的代码。
- #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(char *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 : -123456789
result = -123456789
带字母:
我们可以看到,用我们自己编写的函数运行的结果也是正确的。那么我们该怎么样编写这个函数呢?其实这里主要的知识点是字符串的遍历问题。如果我们想把字符串转化成整型数,那么我们需要一个一个地访问字符串里的内容,即字符串遍历。
首先我们介绍遍历字符串的三种方法:
1. for循环(字符数组)
- #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
在这里我们首先利用了strlen函数测量字符数组的长度,然后用for循环遍历字符串,将输入的字符串的内容一个字符一个字符输出。
2. while循环(字符数组)
- #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
由于输入的字符串的长度是未知的,然而我们遍历字符串的时候需要用到循环,我们知道当循环次数未知时,最好使用while语句。
3.while循环(指针)
- #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};
char *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
在这里我们首先定义了一个指针变量,指向数组的首地址,那为什么要定义这个指针变量呢?为什么不直接用“src++;”呢?
首先,我们要知道的是数组名代表了什么:
①指针常量
②数组首元素的地址
既然数组名代表了指针常量,常量怎么可以自增呢?所以不可以用“src++;”,如果使用“src++;”,那么在编译时便会报错“错误:自增运算中的左值无效”。
以上为遍历字符串的三种方法,希望我们以后可以熟练地运用这三种方法遍历字符串。
在上述“将字符串转化成整型数”的编程题中,还有一个小知识点,就是如何准确地将正数和负数表示出来。首先我们可以利用一个“flag”,我们将flag初始化为1,符号会出现在我们所输入的字符串的首位,只需要判断这个是不是‘-’,如果是的话,将flag置为-1,最后将结果与flag相乘即可,如果是正数,则不用管,正数乘1还是原数。