在這里我們重點介紹遍歷字符串的三種方法。
首先我們來看一道編程題目:
輸入一個字符串,且都是數字,也可以是負數,轉化成相應的整型數並輸出,若輸入字母則停止。
我們知道,在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
- 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
使用“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(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
我們可以看到,用我們自己編寫的函數運行的結果也是正確的。那么我們該怎么樣編寫這個函數呢?其實這里主要的知識點是字符串的遍歷問題。如果我們想把字符串轉化成整型數,那么我們需要一個一個地訪問字符串里的內容,即字符串遍歷。
首先我們介紹遍歷字符串的三種方法:
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
- 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
- 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};
- 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
在這里我們首先定義了一個指針變量,指向數組的首地址,那為什么要定義這個指針變量呢?為什么不直接用“src++;”呢?
首先,我們要知道的是數組名代表了什么:
①指針常量
②數組首元素的地址
既然數組名代表了指針常量,常量怎么可以自增呢?所以不可以用“src++;”,如果使用“src++;”,那么在編譯時便會報錯“錯誤:自增運算中的左值無效”。
以上為遍歷字符串的三種方法,希望我們以后可以熟練地運用這三種方法遍歷字符串。
在上述“將字符串轉化成整型數”的編程題中,還有一個小知識點,就是如何准確地將正數和負數表示出來。首先我們可以利用一個“flag”,我們將flag初始化為1,符號會出現在我們所輸入的字符串的首位,只需要判斷這個是不是‘-’,如果是的話,將flag置為-1,最后將結果與flag相乘即可,如果是正數,則不用管,正數乘1還是原數。
轉載:http://blog.csdn.net/nopoppy/article/details/52613975