如何计算字符串的字符,并将它们分配给C中的不同组(小写和大写)?

时间:2022-01-22 13:05:27

I wrote a program in C that gets a string from the user (50 characters is the limit) and assign the upper-case characters to a string named upper, and the lower-case characters to lower, in the end it supposed to prints those strings (the upper first). My problem is when I enter a string, it only prints one string (i.e if the string starts with an upper character then upper will be printed) instead of two of them.

我用C语言编写了一个程序,它从用户那里获得一个字符串(50个字符是限制),并将大写字符分配给一个名为upper的字符串,小写字符分配给小写字符to lower,最后它应该打印这些字符串(大写的第一个)。我的问题是当我输入一个字符串时,它只打印一个字符串(I。如果字符串以一个大写字符开始,那么将打印upper)而不是两个。

Here's my code:

这是我的代码:

#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;
}

3 个解决方案

#1


1  

You use one counter for two arrays. You increment the counter, no matter which array you fill in. As a result, the first letter to be processed will determine which array will not have its first character as the C string NULL terminator.

对两个数组使用一个计数器。无论填充哪个数组,都要增加计数器。因此,要处理的第一个字母将决定哪个数组的第一个字符不能作为C字符串空结束符。

So? So, when you use printf(), it will stop printing when you use %s, as soon as it meets a NULL terminator, as all the functions of <stdio.h> do. BTW, you had forgotten to include that library.

所以呢?因此,当您使用printf()时,当您使用%s时,当它遇到空终止符时,它将停止打印,就像所有 。顺便说一句,你忘了包括那个图书馆。 的函数一样。h>

One solution would be to use two counters, one for every array and increment the counter of the array that we just filled in.

一种解决方案是使用两个计数器,一个用于每个数组并增加我们刚刚填充的数组的计数器。

Moreover, use >= instead of > to take into account 'a' too. Likewise for 'z', and their capitals.

此外,使用>=而不是>来考虑“a”。z和它们的首都也是如此。

Putting them all together you get something like this:

把它们放在一起,你会得到这样的东西:

#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;
}

Output:

输出:

Georgioss-MacBook-Pro:~ gsamaras$ gcc -Wall main.c
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out 
Enter a string: Samaras
S
amaras

#2


3  

Use different index variables for upper and lower and in your if-statements, change > operator to >= and similarly < to <=

上下使用不同的索引变量,在if语句中,将>操作符更改为>=,类似地将< < <=

#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;
}

#3


0  

You are using j as an iterator for both of your arrays. You don't do that. If you do that, you could have a '\0' at the first place of your other array and it won't be written.

您使用j作为两个数组的迭代器。你不这样做。如果你这样做,你可以在其他数组的第一个位置有一个'\0',它不会被写入。

so you should do that:

所以你应该这样做:

 #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;
    }

Here you are itering on your arrays only when you add one value. Also as said in comments, you should do str[i] >= 'A' && str[i] <= 'Z' not str[i] > 'A' && str[i] < 'Z'

在这里,只有当您添加一个值时,您才会对数组进行处理。也如评论所说,你应该做str[i] >= 'A' & str[i] <= 'Z'而不是str[i] > 'A' & str[i] < 'Z'

#1


1  

You use one counter for two arrays. You increment the counter, no matter which array you fill in. As a result, the first letter to be processed will determine which array will not have its first character as the C string NULL terminator.

对两个数组使用一个计数器。无论填充哪个数组,都要增加计数器。因此,要处理的第一个字母将决定哪个数组的第一个字符不能作为C字符串空结束符。

So? So, when you use printf(), it will stop printing when you use %s, as soon as it meets a NULL terminator, as all the functions of <stdio.h> do. BTW, you had forgotten to include that library.

所以呢?因此,当您使用printf()时,当您使用%s时,当它遇到空终止符时,它将停止打印,就像所有 。顺便说一句,你忘了包括那个图书馆。 的函数一样。h>

One solution would be to use two counters, one for every array and increment the counter of the array that we just filled in.

一种解决方案是使用两个计数器,一个用于每个数组并增加我们刚刚填充的数组的计数器。

Moreover, use >= instead of > to take into account 'a' too. Likewise for 'z', and their capitals.

此外,使用>=而不是>来考虑“a”。z和它们的首都也是如此。

Putting them all together you get something like this:

把它们放在一起,你会得到这样的东西:

#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;
}

Output:

输出:

Georgioss-MacBook-Pro:~ gsamaras$ gcc -Wall main.c
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out 
Enter a string: Samaras
S
amaras

#2


3  

Use different index variables for upper and lower and in your if-statements, change > operator to >= and similarly < to <=

上下使用不同的索引变量,在if语句中,将>操作符更改为>=,类似地将< < <=

#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;
}

#3


0  

You are using j as an iterator for both of your arrays. You don't do that. If you do that, you could have a '\0' at the first place of your other array and it won't be written.

您使用j作为两个数组的迭代器。你不这样做。如果你这样做,你可以在其他数组的第一个位置有一个'\0',它不会被写入。

so you should do that:

所以你应该这样做:

 #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;
    }

Here you are itering on your arrays only when you add one value. Also as said in comments, you should do str[i] >= 'A' && str[i] <= 'Z' not str[i] > 'A' && str[i] < 'Z'

在这里,只有当您添加一个值时,您才会对数组进行处理。也如评论所说,你应该做str[i] >= 'A' & str[i] <= 'Z'而不是str[i] > 'A' & str[i] < 'Z'