如何读取用户输入字符串并将其存储在Array中

时间:2022-11-27 15:43:49

Trying to read user input string from the key board and assign it to an Array. It still confusing.

尝试从键盘读取用户输入字符串并将其分配给数组。它仍然令人困惑。

Also any Idea what char ch = 97 is in this program? Thanks.

还有什么想法char ch = 97在这个程序中?谢谢。

#include<stdlib.h>

int main()
{
    int i = 0;
    int j = 0;
    int count[26]={0};
    char ch = 97;
    char string[100]="readmenow";

    for (i = 0; i < 100; i++)
    {
         for(j=0;j<26;j++)
         {
              if (tolower(string[i]) == (ch+j))
              {
                   count[j]++;
              }
         }
    }
    for(j=0;j<26;j++)
    {
        printf("\n%c -> %d",97+j,count[j]);
    }
}

2 个解决方案

#1


2  

to read user input do this:

阅读用户输入执行此操作:

  #include <stdio.h>  // for fgets
  #include <string.h> // for strlen

  fgets(string,sizeof(string),stdin);
  string[strlen(string)-1] = '\0'; // this removes the \n and replaces it with \0

make sure you include proper headers

确保包含正确的标题

Also ch= 97; is same as doing ch = 'a';

ch = 97;与ch ='a'相同;

EDIT:

scanf is great for reading input as a string as long as the string doesn't have space. fgets is much better

只要字符串没有空格,scanf就非常适合作为字符串读取输入。 fgets要好得多

EDIT 2

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

int main(){

    int i=0,j=0;

    char input[50]; // make the size bigger if you expect a bigger input

    printf("Enter string = ");
    fgets(input,sizeof(input),stdin);
    input[strlen(input)-1] = '\0';

    int count[26]={0};

    for (i = 0; i < strlen(input); i++)
    {
         for(j=0;j<26;j++)
         {
              if (tolower(input[i]) == ('a'+j))
              {
                   count[j]++;
              }
         }
    }
    for(j=0;j<26;j++)
    {
        printf("\n%c -> %d",'a'+j,count[j]);
    }


    return 0;
}

Output: $ ./test

输出:$ ./test

Enter string = this is a test string

a -> 1
b -> 0
c -> 0
d -> 0
e -> 1
f -> 0
g -> 1
h -> 1
i -> 3
j -> 0
k -> 0
l -> 0
m -> 0
n -> 1
o -> 0
p -> 0
q -> 0
r -> 1
s -> 4
t -> 4
u -> 0
v -> 0
w -> 0
x -> 0
y -> 0
z -> 0

#2


0  

 char ch= 97

it means ch='a'
it use ASCII (American Standard Code for Information Interchange)

它意味着ch ='a'它使用ASCII(美国信息交换标准码)

#1


2  

to read user input do this:

阅读用户输入执行此操作:

  #include <stdio.h>  // for fgets
  #include <string.h> // for strlen

  fgets(string,sizeof(string),stdin);
  string[strlen(string)-1] = '\0'; // this removes the \n and replaces it with \0

make sure you include proper headers

确保包含正确的标题

Also ch= 97; is same as doing ch = 'a';

ch = 97;与ch ='a'相同;

EDIT:

scanf is great for reading input as a string as long as the string doesn't have space. fgets is much better

只要字符串没有空格,scanf就非常适合作为字符串读取输入。 fgets要好得多

EDIT 2

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

int main(){

    int i=0,j=0;

    char input[50]; // make the size bigger if you expect a bigger input

    printf("Enter string = ");
    fgets(input,sizeof(input),stdin);
    input[strlen(input)-1] = '\0';

    int count[26]={0};

    for (i = 0; i < strlen(input); i++)
    {
         for(j=0;j<26;j++)
         {
              if (tolower(input[i]) == ('a'+j))
              {
                   count[j]++;
              }
         }
    }
    for(j=0;j<26;j++)
    {
        printf("\n%c -> %d",'a'+j,count[j]);
    }


    return 0;
}

Output: $ ./test

输出:$ ./test

Enter string = this is a test string

a -> 1
b -> 0
c -> 0
d -> 0
e -> 1
f -> 0
g -> 1
h -> 1
i -> 3
j -> 0
k -> 0
l -> 0
m -> 0
n -> 1
o -> 0
p -> 0
q -> 0
r -> 1
s -> 4
t -> 4
u -> 0
v -> 0
w -> 0
x -> 0
y -> 0
z -> 0

#2


0  

 char ch= 97

it means ch='a'
it use ASCII (American Standard Code for Information Interchange)

它意味着ch ='a'它使用ASCII(美国信息交换标准码)