c語言遍歷字符串數組的方法


在這里我們重點介紹遍歷字符串的三種方法。

 
  首先我們來看一道編程題目:
 
  輸入一個字符串,且都是數字,也可以是負數,轉化成相應的整型數並輸出,若輸入字母則停止。
 
  我們知道,在C語言里有一個函數是“atoi”,它可以把字符串轉換成整型數,包含在頭文件stdlib.h中。以下是我們使用了這個函數的代碼。
 
[objc]  view plain  copy
 
  1. #include <stdio.h>  
  2.   
  3. #define MAX_SIZE 1024  
  4.   
  5. int main()  
  6. {  
  7.     char str[MAX_SIZE] = {0};  
  8.       
  9.     int result;  
  10.     int i;  
  11.   
  12.     printf("Please input string : ");  
  13.     gets(str);  
  14.   
  15.     result = atoi(str);  
  16.   
  17.     printf("result = %d\n",result);  
  18.   
  19.     return 0;  
  20. }  
[objc]  view plain  copy
 
  1. #include <stdio.h>  
  2.   
  3. #define MAX_SIZE 1024  
  4.   
  5. int main()  
  6. {  
  7.     char str[MAX_SIZE] = {0};  
  8.       
  9.     int result;  
  10.     int i;  
  11.   
  12.     printf("Please input string : ");  
  13.     gets(str);  
  14.   
  15.     result = atoi(str);  
  16.   
  17.     printf("result = %d\n",result);  
  18.   
  19.     return 0;  
  20. }  
 
運行結果:
 
正數:
[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.   
  3. #define MAX_SIZE 1024  
  4.   
  5. int my_atoi(charchar *str)  
  6. {  
  7.     int i = 0;  
  8.     int result = 0;  
  9.     int flag = 1;  
  10.   
  11.     if (*str == '-')  
  12.     {  
  13.         flag = -1;  
  14.         str++;  
  15.     }  
  16.   
  17.     while (*str != '\0')  
  18.     {  
  19.         if (*str >= '0' && *str <= '9')  
  20.         {  
  21.             result = result * 10 + ( *str - '0' );  
  22.         }  
  23.         else  
  24.         {  
  25.             break;  
  26.         }  
  27.   
  28.         str++;  
  29.     }  
  30.   
  31.     return result *flag;  
  32. }  
  33.   
  34. int main()  
  35. {  
  36.     char str[MAX_SIZE] = {0};  
  37.       
  38.     int result;  
  39.     int i;  
  40.   
  41.     printf("Please input string : ");  
  42.     gets(str);  
  43.   
  44.     result = my_atoi(str);  
  45.   
  46.     printf("result = %d\n",result);  
  47.   
  48.     return 0;  
  49. }  
[objc]  view plain  copy
 
  1. #include <stdio.h>  
  2.   
  3. #define MAX_SIZE 1024  
  4.   
  5. int my_atoi(charchar *str)  
  6. {  
  7.     int i = 0;  
  8.     int result = 0;  
  9.     int flag = 1;  
  10.   
  11.     if (*str == '-')  
  12.     {  
  13.         flag = -1;  
  14.         str++;  
  15.     }  
  16.   
  17.     while (*str != '\0')  
  18.     {  
  19.         if (*str >= '0' && *str <= '9')  
  20.         {  
  21.             result = result * 10 + ( *str - '0' );  
  22.         }  
  23.         else  
  24.         {  
  25.             break;  
  26.         }  
  27.   
  28.         str++;  
  29.     }  
  30.   
  31.     return result *flag;  
  32. }  
  33.   
  34. int main()  
  35. {  
  36.     char str[MAX_SIZE] = {0};  
  37.       
  38.     int result;  
  39.     int i;  
  40.   
  41.     printf("Please input string : ");  
  42.     gets(str);  
  43.   
  44.     result = my_atoi(str);  
  45.   
  46.     printf("result = %d\n",result);  
  47.   
  48.     return 0;  
  49. }  
運行結果:
 
正數:
[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.   
  4. #define MAX_SIZE 1024  
  5.   
  6. int main()  
  7. {  
  8.     char src[MAX_SIZE] = {0};  
  9.   
  10.     int i;  
  11.   
  12.     int len;  
  13.   
  14.     printf("Please input string : ");  
  15.     gets(src);  
  16.   
  17.     len = strlen(src);  
  18.   
  19.     printf("string = ");  
  20.   
  21.     for (i = 0; i < len; i++)  
  22.     {  
  23.         printf("%c",src[i]);  
  24.     }  
  25.   
  26.     printf("\n");  
  27.   
  28.     return 0;  
  29. }  
[objc]  view plain  copy
 
  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. #define MAX_SIZE 1024  
  5.   
  6. int main()  
  7. {  
  8.     char src[MAX_SIZE] = {0};  
  9.   
  10.     int i;  
  11.   
  12.     int len;  
  13.   
  14.     printf("Please input string : ");  
  15.     gets(src);  
  16.   
  17.     len = strlen(src);  
  18.   
  19.     printf("string = ");  
  20.   
  21.     for (i = 0; i < len; i++)  
  22.     {  
  23.         printf("%c",src[i]);  
  24.     }  
  25.   
  26.     printf("\n");  
  27.   
  28.     return 0;  
  29. }  
 
運行結果:
[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.   
  4. #define MAX_SIZE 1024  
  5.   
  6. int main()  
  7. {  
  8.     char src[MAX_SIZE] = {0};  
  9.   
  10.     int i = 0;  
  11.   
  12.     printf("Please input string : ");  
  13.     gets(src);  
  14.   
  15.     printf("string = ");  
  16.   
  17.     while (src[i] != '\0')  
  18.     {  
  19.         printf("%c",src[i]);  
  20.         i++;  
  21.     }  
  22.   
  23.     printf("\n");  
  24.   
  25.     return 0;  
  26. }  
[objc]  view plain  copy
 
  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. #define MAX_SIZE 1024  
  5.   
  6. int main()  
  7. {  
  8.     char src[MAX_SIZE] = {0};  
  9.   
  10.     int i = 0;  
  11.   
  12.     printf("Please input string : ");  
  13.     gets(src);  
  14.   
  15.     printf("string = ");  
  16.   
  17.     while (src[i] != '\0')  
  18.     {  
  19.         printf("%c",src[i]);  
  20.         i++;  
  21.     }  
  22.   
  23.     printf("\n");  
  24.   
  25.     return 0;  
  26. }  
 
運行結果:
[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.   
  4. #define MAX_SIZE 1024  
  5.   
  6. int main()  
  7. {  
  8.     char src[MAX_SIZE] = {0};  
  9.     charchar *temp = src;  
  10.   
  11.     printf("Please input string : ");  
  12.     gets(src);  
  13.   
  14.     printf("string = ");  
  15.   
  16.     while (*temp != '\0')  
  17.     {  
  18.         printf("%c",*temp);  
  19.         temp++;  
  20.     }  
  21.   
  22.     printf("\n");  
  23.   
  24.     return 0;  
  25. }  
[objc]  view plain  copy
 
  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. #define MAX_SIZE 1024  
  5.   
  6. int main()  
  7. {  
  8.     char src[MAX_SIZE] = {0};  
  9.     charchar *temp = src;  
  10.   
  11.     printf("Please input string : ");  
  12.     gets(src);  
  13.   
  14.     printf("string = ");  
  15.   
  16.     while (*temp != '\0')  
  17.     {  
  18.         printf("%c",*temp);  
  19.         temp++;  
  20.     }  
  21.   
  22.     printf("\n");  
  23.   
  24.     return 0;  
  25. }  
 
運行結果:
[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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM