[英]Parsing a String in C, Without Strtok()
我需要通過從C中刪除所有非字母字符來解析C中的字符串。 為此,我要檢查每個字符的ascii值,並確保其在正確的范圍內。 它只是按照我想要的方式工作,所以這不是問題。 但是,我遇到的麻煩是在解析完成后存儲結果字符串。 (順便說一下,我進入C的時間為3周)另外,如果您注意到我對數組使用了奇怪的大小,那是因為我故意使它們變得比需要的大。
char * carry[2]; // This is to simulate argv
carry[1] = "hello1whats2up1"; // 0 is title so I placed at 1
char array[strlen(carry[1])]; // char array of string length
strcpy(array, carry[1]); // copied string to char array
char temp[strlen(carry[1]) + 1]; // Reusable char array
char * finalAnswer[10];
int m = 0, x = 0; // Indexes
if ((sizeof(carry))/8 > 1) { // We were given arguments
printf("Array: %lu\n\n", sizeof(array));
for (int i = 0; i < sizeof(array); i++)
{
if(isalpha(array[i])) { // A-Z & a-z
//printf("%s\n", temp);
temp[x] = array[i]; // Placing chars in temp array
x++;
}
else {
printf("String Length: %lu \nString Name: %s \nWord Index: %d \n\n",
strlen(temp), temp, m); // Testing Purposes
strcpy(finalAnswer[m], temp); // Copies temp into the final answer *** Source of Error
for(int w = 0; w < sizeof(temp); w++) { temp[w] = '\0'; } // Clears temp
x = 0;
m++;
}
}
printf("String Length: %lu \nString Name: %s \nWord Index: %d \n",
strlen(temp), temp, m); // Testing Purposes
strcpy(finalAnswer[m], temp);
for(int w = 0; w < sizeof(temp); w++) { temp[w] = '\0'; } // Clears temp
x = 0;
}
else { printf("No Arguments Given\n"); }
printf("\n");
**編輯
我一直遇到的錯誤是當我嘗試將temp復制到finalAnswer時
**編輯2
我解決了char * finalAnswer [10]遇到的問題
當我嘗試在finalAnswer上使用strcpy時,我從未指定存儲特定字符串所需的大小。 我做完后效果很好。
由於您已經解決了實際的字符串解析問題,因此您的最后評論是我的實際要求。
“ ...我想創建一個長度可變的單詞列表,可以通過索引訪問...”
如果一個人“進入C的三周”,那當然不是容易解決的任務。 表示那是main()
第二個參數的數據結構是:
// array (of unknown size)
// of pointers to char
char * argv[] ;
可以將其寫為指向指針的指針:
// same data structure as char * []
char ** list_of_words ;
這將您直接帶入C的深水。非平凡的C數據結構。 因此,可能需要超過4周的C時間。
但是我們可以發揮創造力。 我們可以使用“內置於C中”一種非平凡的數據結構。 一份文件。
我們可以將單詞寫到文件中。 一字一線。 這就是我們的輸出:存儲在文件中的單詞列表,用換行符分隔。
我們甚至可以想象並編寫一個函數,該函數將從結果中“按索引”讀取單詞。 如您(看來)的需要。
// hint: there is a FILE * behind
int words_count = result_size () ;
const char * word = result_get_word(3) ;
現在,我大膽地前進,並在最后一個“關鍵”部分旁邊寫下了“全部”內容。 畢竟,我相信您也願意做出貢獻。
因此,工作代碼(減去result_size)和result_get_word()仍然有效,並在此處運行: https : //wandbox.org/permlink/uLpAplNl6A3fgVGw
為了避免“可汗之怒”,我也將其粘貼在這里:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
/*
task: remove all non alpha chars from a given string, store the result
*/
int process_and_save (FILE *, const char *) ;
int dump_result(FILE *) ;
int main( const int argc, const char * argv[] )
{
const char * filename = "words.txt";
const char * to_parse = "0abra123ka456dabra789" ;
(void)(&argc) ; (void)argv ; // pacify the compiler warnings
printf("\nInput: %s", to_parse ) ;
int retval = process_and_save(fopen(filename, "w"), to_parse ) ;
if ( EXIT_FAILURE != retval )
{
printf("\n\nOutput:\n") ;
retval = dump_result(fopen(filename, "r"));
}
return retval ;
}
int process_and_save (FILE * fp, const char * input )
{
if(!fp) {
perror("File opening failed");
return EXIT_FAILURE;
}
//
char * walker = (char *)(input) ;
while ( walker++ )
{
if ( ! *walker ) break ;
if ( isalpha(*walker) ) {
fprintf( fp, "%c", *walker ) ;
// I am alpha but next one is not
// so write word end, next
if ( ! isalpha(*(walker +1) ) )
fprintf( fp, "\n" ) ;
}
}
fclose(fp);
return EXIT_SUCCESS;
}
int dump_result(FILE* fp )
{
if(!fp) {
perror("\nFile opening failed");
return EXIT_FAILURE;
}
int c; while ((c = fgetc(fp)) != EOF) { putchar(c); }
if (ferror(fp))
puts("\nI/O error when reading");
fclose(fp);
return EXIT_SUCCESS;
}
我認為這是功能性的,並且可以解析和存儲結果。 不是在復雜的數據結構中,而是在簡單的文件中。 其余的應該很容易。 如果需要幫助,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.