如何计算字符串的字符并将它们分配给 C 中的不同组(小写和大写)
How to count the characters of a string and assign them to different groups (lower case & upper case) in C
我用 C 编写了一个程序,从用户那里获取一个字符串(限制为 50 个字符)并将大写字符分配给名为 upper
的字符串,将小写字符分配给 lower
,最后它应该打印那些字符串(首先是 upper
)。我的问题是当我输入一个字符串时,它只打印一个字符串(即如果字符串以大写字符开头,则将打印 upper
)而不是两个。
这是我的代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#define MAX_LEN 50
int main()
{
char str[MAX_LEN] = { 0 };
char upper[MAX_LEN] = { 0 };
char lower[MAX_LEN] = { 0 };
int i = 0;
int j = 0;
printf("Enter a string: ");
fgets(str, MAX_LEN, stdin);
str[strcspn(str, "\n")] = 0;
for (i = 0; i < strlen(str); i++)
{
if (str[i] > 'A' && str[i] < 'Z')
{
upper[j] = str[i];
}
else if (str[i] > 'a' && str[i] < 'z')
{
lower[j] = str[i];
}
j++;
}
printf("%s", upper);
printf("%s", lower);
getch();
return 0;
}
您正在使用 j 作为两个数组的迭代器。你不那样做。如果你这样做,你可能会在你的另一个数组的第一个位置有一个 '\0',它不会被写入。
所以你应该这样做:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#define MAX_LEN 50
int main()
{
char str[MAX_LEN] = { 0 };
char upper[MAX_LEN] = { 0 };
char lower[MAX_LEN] = { 0 };
int i = 0;
int j = 0, h = 0;
printf("Enter a string: ");
fgets(str, MAX_LEN, stdin);
str[strcspn(str, "\n")] = 0;
for (i = 0; i < strlen(str); i++)
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
upper[j++] = str[i];
}
else if (str[i] > 'a' && str[i] < 'z')
{
lower[h++] = str[i];
}
}
printf("%s", upper);
printf("%s", lower);
return 0;
}
此处仅当您添加一个值时才对数组进行迭代。
同样如评论中所述,您应该 str[i] >= 'A' && str[i] <= 'Z'
而不是 str[i] > 'A' && str[i] < 'Z'
您对两个数组使用一个计数器。无论您填写哪个数组,您都会递增计数器。因此,要处理的第一个字母将决定哪个数组 not 将其第一个字符作为 C 字符串 NULL 终止符。
所以呢?因此,当您使用 printf()
时,它会在您使用 %s
时停止打印,只要它遇到 NULL 终止符,就像 <stdio.h>
的所有函数一样。顺便说一句,您忘记包含该库了。
一个解决方案是使用两个计数器,每个数组一个,并递增我们刚刚填充的数组的计数器。
此外,使用 >=
而不是 >
来考虑 'a'。 'z' 及其首都也是如此。
把它们放在一起你会得到这样的东西:
#include <string.h>
#include <time.h>
#include <math.h>
#include <stdio.h> // you hadn't include that!
#define MAX_LEN 50
int main()
{
char str[MAX_LEN] = { 0 };
char upper[MAX_LEN] = { 0 };
char lower[MAX_LEN] = { 0 };
int i = 0;
int j = 0; // counter for 'upper'
int k = 0; // counter for 'lower'
printf("Enter a string: ");
fgets(str, MAX_LEN, stdin);
str[strcspn(str, "\n")] = 0;
for (i = 0; i < strlen(str); i++)
{
if (str[i] >= 'A' && str[i] <= 'Z') // use the equal operator as well for reading 'A' and 'Z' as well
{
upper[j++] = str[i]; // increment the counter 'j'
}
else if (str[i] >= 'a' && str[i] <= 'z') // use the equal operator as well for reading 'a' and 'z' as well
{
lower[k++] = str[i]; // increment the counter 'k'
}
}
// print your strings, but use a newline for aesthetics
printf("%s\n", upper);
printf("%s\n", lower);
return 0;
}
输出:
Georgioss-MacBook-Pro:~ gsamaras$ gcc -Wall main.c
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out
Enter a string: Samaras
S
amaras
对 upper
和 lower
使用不同的索引变量,并在您的 if-statements
中,将 >
运算符更改为 >=
,类似地 <
至 <=
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#define MAX_LEN 50
int main()
{
char str[MAX_LEN] = { 0 };
char upper[MAX_LEN] = { 0 };
char lower[MAX_LEN] = { 0 };
int i = 0;
int up = 0, low = 0;
printf("Enter a string: ");
fgets(str, MAX_LEN, stdin);
str[strcspn(str, "\n")] = 0;
for (i = 0; i < strlen(str); i++)
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
upper[up] = str[i];
up++;
}
else if (str[i] >= 'a' && str[i] <= 'z')
{
lower[low] = str[i];
low++;
}
}
printf("%s\n", upper);
printf("%s", lower);
getch();
return 0;
}
我用 C 编写了一个程序,从用户那里获取一个字符串(限制为 50 个字符)并将大写字符分配给名为 upper
的字符串,将小写字符分配给 lower
,最后它应该打印那些字符串(首先是 upper
)。我的问题是当我输入一个字符串时,它只打印一个字符串(即如果字符串以大写字符开头,则将打印 upper
)而不是两个。
这是我的代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#define MAX_LEN 50
int main()
{
char str[MAX_LEN] = { 0 };
char upper[MAX_LEN] = { 0 };
char lower[MAX_LEN] = { 0 };
int i = 0;
int j = 0;
printf("Enter a string: ");
fgets(str, MAX_LEN, stdin);
str[strcspn(str, "\n")] = 0;
for (i = 0; i < strlen(str); i++)
{
if (str[i] > 'A' && str[i] < 'Z')
{
upper[j] = str[i];
}
else if (str[i] > 'a' && str[i] < 'z')
{
lower[j] = str[i];
}
j++;
}
printf("%s", upper);
printf("%s", lower);
getch();
return 0;
}
您正在使用 j 作为两个数组的迭代器。你不那样做。如果你这样做,你可能会在你的另一个数组的第一个位置有一个 '\0',它不会被写入。
所以你应该这样做:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#define MAX_LEN 50
int main()
{
char str[MAX_LEN] = { 0 };
char upper[MAX_LEN] = { 0 };
char lower[MAX_LEN] = { 0 };
int i = 0;
int j = 0, h = 0;
printf("Enter a string: ");
fgets(str, MAX_LEN, stdin);
str[strcspn(str, "\n")] = 0;
for (i = 0; i < strlen(str); i++)
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
upper[j++] = str[i];
}
else if (str[i] > 'a' && str[i] < 'z')
{
lower[h++] = str[i];
}
}
printf("%s", upper);
printf("%s", lower);
return 0;
}
此处仅当您添加一个值时才对数组进行迭代。
同样如评论中所述,您应该 str[i] >= 'A' && str[i] <= 'Z'
而不是 str[i] > 'A' && str[i] < 'Z'
您对两个数组使用一个计数器。无论您填写哪个数组,您都会递增计数器。因此,要处理的第一个字母将决定哪个数组 not 将其第一个字符作为 C 字符串 NULL 终止符。
所以呢?因此,当您使用 printf()
时,它会在您使用 %s
时停止打印,只要它遇到 NULL 终止符,就像 <stdio.h>
的所有函数一样。顺便说一句,您忘记包含该库了。
一个解决方案是使用两个计数器,每个数组一个,并递增我们刚刚填充的数组的计数器。
此外,使用 >=
而不是 >
来考虑 'a'。 'z' 及其首都也是如此。
把它们放在一起你会得到这样的东西:
#include <string.h>
#include <time.h>
#include <math.h>
#include <stdio.h> // you hadn't include that!
#define MAX_LEN 50
int main()
{
char str[MAX_LEN] = { 0 };
char upper[MAX_LEN] = { 0 };
char lower[MAX_LEN] = { 0 };
int i = 0;
int j = 0; // counter for 'upper'
int k = 0; // counter for 'lower'
printf("Enter a string: ");
fgets(str, MAX_LEN, stdin);
str[strcspn(str, "\n")] = 0;
for (i = 0; i < strlen(str); i++)
{
if (str[i] >= 'A' && str[i] <= 'Z') // use the equal operator as well for reading 'A' and 'Z' as well
{
upper[j++] = str[i]; // increment the counter 'j'
}
else if (str[i] >= 'a' && str[i] <= 'z') // use the equal operator as well for reading 'a' and 'z' as well
{
lower[k++] = str[i]; // increment the counter 'k'
}
}
// print your strings, but use a newline for aesthetics
printf("%s\n", upper);
printf("%s\n", lower);
return 0;
}
输出:
Georgioss-MacBook-Pro:~ gsamaras$ gcc -Wall main.c
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out
Enter a string: Samaras
S
amaras
对 upper
和 lower
使用不同的索引变量,并在您的 if-statements
中,将 >
运算符更改为 >=
,类似地 <
至 <=
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#define MAX_LEN 50
int main()
{
char str[MAX_LEN] = { 0 };
char upper[MAX_LEN] = { 0 };
char lower[MAX_LEN] = { 0 };
int i = 0;
int up = 0, low = 0;
printf("Enter a string: ");
fgets(str, MAX_LEN, stdin);
str[strcspn(str, "\n")] = 0;
for (i = 0; i < strlen(str); i++)
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
upper[up] = str[i];
up++;
}
else if (str[i] >= 'a' && str[i] <= 'z')
{
lower[low] = str[i];
low++;
}
}
printf("%s\n", upper);
printf("%s", lower);
getch();
return 0;
}