我試圖制作一個程式來檢查 arr2 是否在 arr1 內,然后列印結果。
我寫了這個:
#include <stdio.h>
#define N 10
int main() {
char arr1[10];
char arr2[10];
int i, j, number_of_arr1, number_of_arr2;
printf("Please enter up to %d characters\n", N);
for (i=0; i<N; i) {
scanf(" %c", &arr1[i]);
if (arr1[i] != 0) {
number_of_arr1;
}
}
printf("Please enter up to %d characters again\n", N);
for (i=0; i<N; i) {
scanf(" %c", &arr2[i]);
if (arr2[i] != 0) {
number_of_arr2;
}
}
for (i = 0; j < number_of_arr2; j) {
for (j = 0; arr2[j] != arr1[i]; i) {
if (i == number_of_arr1) {
printf("The second array is not within the first array");
break;
}
}
i = 0;
}
printf("The second array is within the first array");
return 0;
}
當我輸入例如“asdfghjklq”而不是“asd”或“w”時沒有輸出。
還有一件事,如果第一次輸入少于 10 個字符(例如“asd”),程式就會卡住。
uj5u.com熱心網友回復:
問題是你的回圈
for (i=0; i<N; i) {
scanf(" %c", &arr2[i]);
if (arr2[i] != 0) {
number_of_arr2;
}
}
將始終嘗試準確讀取 10 個非空白字符。ENTER即使在用戶按下鍵之后,它也會繼續嘗試讀取那么多字符。這就解釋了為什么您的程式似乎被卡住并且沒有輸出:您的程式仍在等待用戶輸入 10 個非空白字符。在除錯器中逐行運行程式時,這種行為清晰可見。
問題是該函式scanf通常將換行符視為任何其他空白字符。因此,它不會將換行符視為與空格字符有任何不同。這不是你想要的行為。您希望程式在scanf遇到換行符時立即跳出回圈。
為了讀取單行輸入,我建議您使用函式fgets而不是scanf,并將回圈替換為以下代碼:
char *p;
//attempt to read one line of input
if ( fgets( arr2, sizeof arr2, stdin ) == NULL )
{
fprintf( stderr, "Input error!\n" );
exit( EXIT_FAILURE );
}
//find the newline character
p = strchr( arr2, '\n' );
if ( p == NULL )
{
//the reason why we did not find a newline character was
//probably because the user input was too long to fit into
//the array
fprintf( stderr, "Too many characters!\n" );
exit( EXIT_FAILURE );
}
//remove the newline character by overwriting it with a
//terminating null character
*p = '\0';
//calculate length of string
number_of_arr2 = p - arr2;
請注意,您必須這樣做才能#include <stdlib.h>使#include <string.h>此代碼正常作業。
但是,此代碼有一個問題:該函式fgets適用于以空字符結尾的字串,這意味著字符陣列不僅需要 10 個字符的空間,還需要空字符結尾的空間。此外,該函式還fgets存盤換行符(這很有用,因此您可以查看是否讀入了完整的行)。因此,一個陣列大小10是不夠的,因為它只能存盤8除換行符和終止空字符之外的字符。如果您希望能夠存盤10字符,則必須將陣列的大小arr2增加到12.
在應用上述修復后,將上面的代碼放入自己的函式中,輸入兩個最多 10 個字符的字串的代碼應如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10
//NOTE: When using this function, buffer_size must have 2 bytes
//more than the actual string length, because it needs space for
//the newline character (which will be removed) and the
//terminating null character.
//This function will return the length of the string without the
//newline character.
int get_line_from_user( char *buffer, int buffer_size );
int main()
{
char arr1[12];
char arr2[12];
int number_of_arr1, number_of_arr2;
//read the first string and determine its length
printf( "Please enter up to %d characters: ", N );
number_of_arr1 = get_line_from_user( arr1, sizeof arr1 );
//read the second string
printf( "Please enter up to %d characters again: ", N );
number_of_arr2 = get_line_from_user( arr2, sizeof arr2 );
//print the input
printf(
"\n"
"String 1:\n"
"Length: %d\n"
"Content: %s\n"
"\n"
"String 2:\n"
"Length: %d\n"
"Content: %s\n"
"\n",
number_of_arr1, arr1, number_of_arr2, arr2
);
}
int get_line_from_user( char *buffer, int buffer_size )
{
char *p;
//attempt to read one line of input
if ( fgets( buffer, buffer_size, stdin ) == NULL )
{
fprintf( stderr, "Input error!\n" );
exit( EXIT_FAILURE );
}
//find the newline character
p = strchr( buffer, '\n' );
if ( p == NULL )
{
//the reason why we did not find a newline character was
//probably because the user input was too long to fit into
//the array
fprintf( stderr, "Too many characters!\n" );
exit( EXIT_FAILURE );
}
//remove the newline character by overwriting it with a
//terminating null character
*p = '\0';
//return the length of the string
return p - buffer;
}
該程式具有以下行為:
Please enter up to 10 characters: 01234567890
Too many characters!
Please enter up to 10 characters: 0123456789
Please enter up to 10 characters again: 123
String 1:
Length: 10
Content: 0123456789
String 2:
Length: 3
Content: 123
如您所見,該程式現在能夠接受少于 10 個字符的輸入。
我沒有包括您的程式中試圖確定第二個字串是否是第一個字串的一部分的部分,因為您的程式的那部分沒有意義。您正在使用變數的值j,盡管該值是不確定的,因為您沒有對其進行初始化。
由于該問題與您的問題(程式卡住)中提到的問題完全不同,因此我不會在回答中進一步討論此問題。但是,請隨時就該問題提出一個新問題,但我建議您先閱讀以下內容:
如何除錯小程式?
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/485438.html
