簡體   English   中英

在char數組中搜索char

[英]Searching char array for char

我一直在嘗試遍歷預定的字符數組,並將其與掃描到的單個字符進行比較。 如果掃描的字符在數組中,我想將其添加到2d數組中(如果不在數組中),我想進行錯誤處理。

我的代碼是當前

    char c;
    char legalChar[] = "./\\=@ABCDEFGHIJKLMNOPQRSTUVWXYZ\n";
    int rowCount = 0;
    int colCOunt = 0;
    int i = 0;
    FILE * map;

    while (c = fgetc(map), c != EOF) {
        while (i < (sizeof(legalChar))){
            if (c == legalChar[i]){
                if (c == '\n'){
                    /*Add 1 to number of rows and start counting columns again */
                    rowCount++;
                    colCount = 0;
                }
                else {
                    /*would have code to add char to 2d array here */
                    colCount++;
                }
            }
        i++;
    }

我計划有

    if (c != legalChar[i]){
        /*Error handling */
    }

但這是行不通的,因為它在每次迭代時都會跳入此if語句。

目前程序的輸出是colCount被分配為1,rowCount保持為0。所有經過迭代的char都在legalChar []數組內部,所以我不確定自己在做什么錯。

任何建議將不勝感激。

謝謝

您的問題是if (c != legalChar[i])幾乎始終為true。 假設輸入的字符是M ,這顯然在legalChar 如果您檢查c != legalChar[i] ,那么您正在檢查c != '.' 第一次,這顯然是事實。

處理此問題的更好方法是使標志值以false開頭,如果發現有問題,將其設置為true。 一旦完成循環,如果該標志仍為false,則說明未找到該值。

此外,每次循環時都應重置ifor循環比while循環更有意義,尤其是在使用c99 ,因為i可以在循環中聲明。

int c;
char legalChar[] = "./\\=@ABCDEFGHIJKLMNOPQRSTUVWXYZ\n";
int rowCount = 0;
int colCOunt = 0;
int i = 0;
int found = 0;
FILE * map;

while (c = fgetc(map), c != EOF) {
    found = 0;
    for (i = 0; i < sizeof(legalChar); i++){
        if (c == legalChar[i]){
            if (c == '\n'){
                /*Add 1 to number of rows and start counting columns again */
                rowCount++;
                colCount = 0;
            }
            else {
                /*would have code to add char to 2d array here */
                colCount++;
            }
            found = 1;
            // break out of loop here?
        }
    }
    if (!found) {
        // Error handling here
    }
}

在這里,我做了一些測試,我的代碼運行良好,也許您可​​以嘗試:

#include "stdio.h"
  2 int main()                                                                                                                                              
  3 {
  4     char c;
  5     char legalChar[33] = "./\\=@ABCDEFGHIJKLMNOPQRSTUVWXYZ\n";
  6     int rowCount = 0;
  7     int colCount = 0;
  8     int i = 0;
  9     FILE * map;
 10     if(NULL ==(map = fopen("error.txt","r")))
 11             return -1;
 12     int is_ok; 
 13     while (!feof(map))
 14     {
 15          c = fgetc(map);
 16          i = 0;
 17          is_ok = 0;
 18          while (i < (sizeof(legalChar)))
 19          {
 20             if (c == legalChar[i])
 21             {
 22                 if(c == '\n')
 23                 {
 24                     rowCount++;
 25                     colCount = 0;
 26                 }   
 27                 else
 28                 {
 29                     colCount++;
 30                 }   
 31                 is_ok = 1;
 32             }   
 33             else
 34             {
 35                 if(i == 31&&is_ok == 0)// not the char in legalChar
 36                 {
 37                     printf("This char %c is ilegal in %d ,%d \n",c,rowCount,colCount);
 38                     colCount++;
 39                 }  
 40             }
 41             ++i;
 42         }
 43     }  
 44     return 0;
 45 } 

我認為您可以使用strchr簡化此代碼:

    char c;
    char legalChar[] = "./\\=@ABCDEFGHIJKLMNOPQRSTUVWXYZ\n";
    FILE * map;
    int legal = 1;

    while (c = fgetc(map) && c != EOF && 1 == legal) {
        if (NULL == strchr(legalChar, c)) {
           legal = 0;
           // Error message pointing out invalid character
        }
        else {
           // Add to array
        }
    }

一旦發現無效字符,以上內容將中止循環。 如果您不想這樣做,只需刪除對“ legal”變量的所有引用即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM