繁体   English   中英

如何在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;
}

使用不同的指数变量upperlower ,并在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字符串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

您正在使用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'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM