计数字符和字符串n c

发布于 2025-02-09 20:07:50 字数 801 浏览 3 评论 0原文

这是我班级的任务之一,这是作业的目的:

编写一个程序,其输入是字符和字符串,并且其输出表示字符中出现在字符串中的次数。输出应包括输入字符并使用复数形式, n's ,如果字符出现的次数为不是 完全1。您可以假设字符串不包含空格,并且始终包含少于50个字符。

这是我到目前为止的代码,我是C编程的新手,因此我还不知道如何正确声明字符串。到目前为止,我了解到C中没有字符串,就像Java中的字符串

#include <stdio.h>
#include <string.h>

int main(void) {

    char userChar;
    char userString[50];
    int count = 0;
      
    for (int i = 0; i < userChar; i++) {
        if (userString[i] == userChar)
            count++;
    }
      
    printf("%d", count);
    if (count != 1)
        printf("'s");
      
    return 0;
}

一样

我需要更改代码,从n星期一 1 n

这是我唯一获得的输出,它只输出了一个正确的事情:

0's

This is one of the assignments for my class and this is the objective of the assignment:

Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. The output should include the input character and use the plural form, n's, if the number of times the characters appears is not exactly 1. You may assume that the string does not contain spaces and will always contain less than 50 characters.

This is the code I have so far and I am new to C programming so I don't know how to declare Strings correctly just yet. So far I learned there are no strings in C like there is in Java and you have to do them as a character array:

#include <stdio.h>
#include <string.h>

int main(void) {

    char userChar;
    char userString[50];
    int count = 0;
      
    for (int i = 0; i < userChar; i++) {
        if (userString[i] == userChar)
            count++;
    }
      
    printf("%d", count);
    if (count != 1)
        printf("'s");
      
    return 0;
}

For example, if I wanted to input n Monday and output 1 n

What would I need to change in my code to go from n Monday to 1 n

This is the only output I am getting, and it only has outputted one thing correctly:

0's

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

讽刺将军 2025-02-16 20:07:50

首先,我希望这不被视为作弊:-)

第二,您需要将用户查看和用户串定为主要参数,然后在运行时传递它们。他们什么也没分配,所以这就是为什么您获得

0的

第三,您的 条件是错误的。您需要它,因此它仅在字符串的长度上迭代:

for(int i = 0; i&lt; strlen(userString); i ++)

最后,您没有在以前打印用户char的值回报

First, I hope this is not considered cheating :-)

Second, you need to define userChar and userString as arguments for main, and pass them in at run time. They are assigned nothing, so that is why you get

0's

Third, your for condition is wrong. You need this so it only iterates through the length of the string:

for (int i = 0; i < strlen(userString); i++)

Finally, You are not printing the value of userChar prior to the return

咽泪装欢 2025-02-16 20:07:50

首先,您需要输入字符串和一个字符。要计算字符串中字符的出现数量,您可以使用标准字符串函数strchr

该程序看起来像以下内容

#include <stdio.h>
#include <string.h>


int main(void) 
{
    char userChar = ' ';
    char userString[50] = "";
  
    printf( "Enter a string without embedded spaces\nof the length less than %d: ", 50 );
    scanf( "%49s", userString );

    printf( "Enter a character to search in the string: " );
    scanf( " %c", &userChar );

    size_t n = 0;

    for ( const char *p = userString; ( p = strchr( p, userChar ) ) != NULL; ++p )
    {
        ++n;
    }
     
    printf( "%zu%s %c\n", n, n < 2 ? "" : "'s", userChar ); 
}

At first you need to input a string and a character. To count the number of occurrences of the character in the string you can use standard string function strchr.

The program can look something like the following

#include <stdio.h>
#include <string.h>


int main(void) 
{
    char userChar = ' ';
    char userString[50] = "";
  
    printf( "Enter a string without embedded spaces\nof the length less than %d: ", 50 );
    scanf( "%49s", userString );

    printf( "Enter a character to search in the string: " );
    scanf( " %c", &userChar );

    size_t n = 0;

    for ( const char *p = userString; ( p = strchr( p, userChar ) ) != NULL; ++p )
    {
        ++n;
    }
     
    printf( "%zu%s %c\n", n, n < 2 ? "" : "'s", userChar ); 
}
苦行僧 2025-02-16 20:07:50

预期的输出不是0的,它应包括计数字符:例如,如果字符为n和字符串 nourtay ,则输出应应BE

1 n

,如果字符串为eeny-Miny-Miny-Miny-Moe,则输出将

3 n's

在这里是一个修改版本:

#include <stdio.h>

int main() {

    char userChar;
    char userString[50];
    int i, count;

    printf("Enter character: ");
    scanf(" %c", &userChar);
    printf("Enter string (single word): ");
    // read a word with at most 49 characters
    scanf(" %49s", userString);
    
    count = 0;
    for (i = 0; userString[i] != '\0'; i++) {
        if (userString[i] == userChar)
            count++;
    }
      
    printf("%d %c", count, userChar);
    if (count != 1)
        printf("'s");
    printf("\n");
      
    return 0;
}

The expected output is not 0's, it should include the counted character: for example if the character is n and the string Monday, the output should be

1 n

and if the string is Eeny-meeny-miny-moe, the output would be

3 n's

Here is a modified version:

#include <stdio.h>

int main() {

    char userChar;
    char userString[50];
    int i, count;

    printf("Enter character: ");
    scanf(" %c", &userChar);
    printf("Enter string (single word): ");
    // read a word with at most 49 characters
    scanf(" %49s", userString);
    
    count = 0;
    for (i = 0; userString[i] != '\0'; i++) {
        if (userString[i] == userChar)
            count++;
    }
      
    printf("%d %c", count, userChar);
    if (count != 1)
        printf("'s");
    printf("\n");
      
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文