[英]Program prints unrelated chars
I wanted to split an array to 2 arrays that the first one contains the lowercased letters of the original array and the second one contains the uppercased letters and from some reason it prints some unrelated chars. 我想将一个数组拆分为2个数组,第一个数组包含原始数组的小写字母,第二个数组包含大写字母,并且由于某种原因,它会打印一些不相关的字符。
#include <stdio.h> #include <string.h> #define LEN 8 int main(void) { char str[] = "SHaddOW"; char smallStr[LEN], bigStr[LEN]; int i = 0; int indexSmall = 0; int indexBig = 0; for (i = 0; i <= LEN; i++) { if (str[i] <= 'Z') { smallStr[indexSmall] = str[i]; indexSmall++; } if (str[i] >= 'Z') { bigStr[indexBig] = str[i]; indexBig++; } } printf("1: "); puts(smallStr); printf("2: "); puts(bigStr); system("PAUSE"); return 0; }
Don't define length before you create the string to test. 在创建要测试的字符串之前,请不要定义长度。 Create it's length after defining the string to test.
在定义要测试的字符串后,创建它的长度。
Copy the characters as you encounter them, but as @Ed Heal says you must add a null terminator so that you can print out the two strings (they aren't really strings until they are null terminated). 在遇到字符时复制它们,但是正如@Ed Heal所说,必须添加一个空终止符,以便可以打印出两个字符串(直到它们以空终止为止,它们才是真正的字符串)。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main (void)
{
char str[] = "SHaddOW";
int len = strlen(str) +1;
char smallStr[len], bigStr[len];
char term[] = {'\0'};
int n, s, b;
s=0;
b=0;
for(n=0; n<len; n++) {
if(islower(str[n])) {
memcpy(smallStr +s, str +n, 1);
s++;
} else if (isupper(str[n])){
memcpy(bigStr +b, str +n, 1);
b++;
}
}
memcpy(smallStr + s, term, 1);
memcpy(bigStr + b , term, 1 );
printf("Upper: %s\n", bigStr);
printf("Lower: %s\n", smallStr);
}
Output: 输出:
Upper: SHOW 上:SHOW
Lower: add 降低:添加
Add this to the if structure (and other code to support it) 将此添加到if结构(以及其他支持它的代码)
} else {
memcpy(anyStr +a, str +n, 1);
a++;
}
then: char str[] = ".S1H2a3d4d5O6W."; 然后:char str [] =“ .S1H2a3d4d5O6W。”;
and: 和:
printf("Anything else: %s\\n", anyStr); printf(“其他:%s \\ n”,anyStr);
returns: 收益:
Upper: SHOW 上:SHOW
Lower: add 降低:添加
Anything else: .123456. 其他:.123456。
A more compact approach with (perhaps) more meaningful variable names: 具有(可能)更有意义的变量名的更紧凑的方法:
#include <stdio.h>
#include <ctype.h>
#include <stdint.h>
#include <string.h>
int main ( void ) {
const char str[] = "SHaddOW";
size_t len = strlen(str); /* better to get actual length */
char lowers[len + 1]; /* add one for the nul char */
char uppers[len + 1]; /* (see below) */
int c;
int i = 0;
int n_upper = 0;
int n_lower = 0;
while ((c = str[i++]) != '\0') {
if (isupper(c)) uppers[n_upper++] = c; /* no need to reinvent */
if (islower(c)) lowers[n_lower++] = c; /* the wheel here */
}
uppers[n_upper] = '\0'; /* the nul char ('\0') marks */
lowers[n_lower] = '\0'; /* the end of a C "string" */
printf("1: %s\n", lowers);
printf("2: %s\n", uppers);
return 0;
}
If you are super concerned about efficiency you could add an else
before if (islower...
如果您非常担心效率,可以在
if (islower...
之前添加else
if (islower...
Adding const
means you "promise" the characters in the array won't be changed. 添加
const
意味着您“承诺”数组中的字符将不会更改。
The type size_t
is an integer type, but may be larger than int
. size_t
类型是整数类型,但可以大于int
。 It is the correct type for the return of strlen()
. 这是返回
strlen()
的正确类型。 It is defined in <stdint.h>
. 它在
<stdint.h>
定义。 None the less, using int
will almost always work (on most systems a string would have to be 'yooooge' for its length to be bigger than an int can hold) . 尽管如此,使用
int
几乎总是可以工作的(在大多数系统上,字符串的长度必须大于int可以容纳的范围,因此必须“ yooooge”) 。
The variable c
is declared as int
instead of char
because int
is the proper type for the isXXXXX()
functions (which are defined in <ctype.h>
) . 变量
c
被声明为int
而不是char
因为int
是isXXXXX()
函数(在<ctype.h>
中定义 isXXXXX()
的正确类型。 It is also a good habit to get into because of the parallels between this loop and another common idiom while ((c = fgetc(fp)) != EOF) ...
. 由于此循环与另一个常见习语之间的并行性
while ((c = fgetc(fp)) != EOF) ...
这也是一个好习惯。
You should consider using isupper()
and islower()
functions. 您应该考虑使用
isupper()
和islower()
函数。 Code would be cleaner. 代码会更干净。 And what if you have some non alpha characters?
如果您有一些非字母字符怎么办? Your conditions won't work.
您的条件不起作用。
for (i = 0; i < LEN; i++)
{
if (islower(str[i]))
{
smallStr[indexSmall] = str[i];
indexSmall++;
}
else if (isupper(str[i]))
{
bigStr[indexBig] = str[i];
indexBig++;
}
}
As @Ed Heal mention. 就像@Ed Heal提到的那样。 To avoid printing rubbish, after for loopt you should add a null characters to arrays.
为避免打印垃圾,在for loopt之后,应在数组中添加一个空字符。
smallStr[indexSmall] = '\0';
bigStr[indexBig] = '\0';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.