Home > Back-end > C language two-dimensional arrays, character array
C language two-dimensional arrays, character array
Time:04-23
# include # include Int main () { Int n, m; Char a, [100]. The scanf (" % d ", & amp; N); Gets (a); Char b [1000] [n]. Char c [100]; Int len=strlen (a); Int h=0, g=0; for(int i=0; iC=[h] a [I]; H++; If (h==n) { H=0; [g] strcpy (b, c);
//printf (" % s \ n ", [g] b); G + +; }
} Printf (" % s \ n ", "b" [0]).
return 0; } I want to write an input a string, an integer, the string of per n a "group", is I use a two-dimensional array storage per n characters each line, form a string, such as the input string abcdefgh with an integer 4. Corresponding assigned into a two-dimensional array is s [0]="abcd", s [1]="efgh", (s) as a two-dimensional array Part of the code above comment out//printf (" % s \ n ", b [g]); If not be comments, can be normal output, if the code above the final output again, can appear problem, why ah!
CodePudding user response:
For '\ 0' terminator character array is the string; Array b, c is not a string, use strcpy, % s the operating problems,
CodePudding user response:
Char b [1000] [n + 1); . [g] strcpy (b, c); Strcpyt (& amp; (b [g] [n]), '\ 0'); Seems to be a little small problems, oneself to achieve these functions, not difficult also
CodePudding user response:
# include #include # include
# define STR_LEN 128
Int main () { Int I, j, n, CNT; Int len, row, col. Char STR [STR_LEN]; Char * * PSTR;
The fgets (STR, STR_LEN stdin); The scanf (" % d ", & amp; N); If (n & gt;=STR_LEN - 1) N=STR_LEN - 1;
Len=strlen (STR); If (len & lt; STR_LEN) Len -=1; The row=len/n + 1; Col=n + 1; PSTR=(char * *) malloc (sizeof (char *) * row + 1); if (! PSTR) exit(0);
for (i=0; I & lt; The row. I++) { PSTR=[I] (char *) malloc (sizeof (char) * col); if (! PSTR [I]) exit(0); }
i=0; CNT=0; While (I & lt; Len & amp; & STR [I]) { For (j=0; J & lt; n; J++) PSTR [CNT] [j]=STR [I + j]; PSTR [CNT] [j]=0; I +=n; cnt++; }
for (i=0; I & lt; CNT. I++) Puts (PSTR [I]);
/* Int n, m; Char a, [100]. The scanf (" % d ", & amp; N); Gets (a); B//char [1000] [n]. B//char [1000] [n]. Char b [100] [100]. Char c [100]; Int len=strlen (a); Int h=0, g=0; for(int i=0; iC=[h] a [I]; H++; If (h==n) { H=0; [g] strcpy (b, c);
//printf (" % s \ n ", [g] b); G + +; }
} Printf (" % s \ n ", "b" [0]). */ return 0; }
For your reference ~
Call strcpy is the premise of c is a string that end with '\ 0', Suggest know the strcpy, In addition, n is the input, so you can't do the column number of the array, suggest using dynamic application space, can save a lot of space,