Home > Back-end >  The for loop reads n strings
The for loop reads n strings

Time:09-16

I use the following code read n strings and capitalize the first letter of each string, behind of all lowercase,
When I enter
2
Saca (enter)
Program is run directly, I don't understand why the input that there will be a problem, the input of other seems to be output correctly,
Please answer, look at the code is what's the problem??
# include
# include
Int main ()
{
Int I, j, N.
Char a, [N] [100].
The scanf (" % d ", & amp; N);
//getchar ();
for(i=0; i{
The scanf (" % s ", a [I]);
//getchar ();
}
for(i=0; i{
for(j=0; J{
If (j==0)
{
If (a [I] [j] & gt; 'a')
A [I] [j]=[I] a [j] - 32;

}
The else
{
If (a [I] [j] <'a')
A [I] [j]=[I] a [j] + 32;

}
}
Printf (" % s \ n ", a [I]);
}

}

CodePudding user response:

First getchar place to let go of annotation, that means you need to perform getchar (it's role is to empty a newline, otherwise you press enter is equivalent to receive two data, a data correctly, a newline), will you input data, N receive 2, a [0] (the first for loop) to receive a newline, then a [1] receive saca (the second for loop), the for loop is over, so the program can run directly,
Second, do not use variables to define the length of the array, namely a [N] [100] into a [100] [100], to be use variables to define the array length, the application of memory, your program can run normally, because of a an array, memory didn't cause conflict, you now, so it is not safe to use,

CodePudding user response:

N't initialize

CodePudding user response:

I see, thank you for your bosses

CodePudding user response:

reference 1st floor qybao response:
getchar first place to let go, that means you need to perform getchar (it's role is to empty a newline, otherwise you press enter is equivalent to receive two data, a data correctly, a newline), will you input data, N receive 2, a [0] (the first for loop) to receive a newline, then a [1] receive saca (the second for loop), the for loop is over, so the program can run directly,
Second, do not use variables to define the length of the array, namely a [N] [100] into a [100] [100], to be use variables to define the array length, the application of memory, your program can run normally, because of a an array, memory didn't cause conflict, you now, so it is not safe to use,

Don't need to let go, is not gets the scanf % s or the fgets, so there is no need to getchar ();

Char a, [N] [100]. Here N is unknown, need to determine the length of the array, it is recommended that the original poster N with a macro definition
  • Related