1

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.

#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]);
    }
}
1
  • char ch = 97; -- 97 is ASCII for 'a'. Commented Nov 3, 2013 at 2:29

2 Answers 2

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';

EDIT:

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

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

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
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for that ch = 97 is cleared now.. When reading user input string from keyboard cant we use scanf ???
Ok.. What I'm trying to do is.. to add code lines to above program to read a input from key board and assign to the strring array and display the results. for example when I entered " How are you today" just need to assign it to that array and count and display each characters in there. Thanks for helping.
Hey Mate, Thats great.. Tried but it giving me hard time to get a out put like the code I posted erlier.. I want to read the key board input string store it in the array and give the out put like this a - 1 b - 0 c - 0 d - 0 e - 2 etc etc.. instead of hardcode the string into the chat string[100]="readmenow" need to get it from the keyboard and display the results like this.. Thanks mate
dude I don't know what you are looking for ... do you want the difference of each character from 'a'? or do you want the number of times each letter is present in that string?
@Asanka'cj'Munasinghe is that what you wanted ? or you only want to print out the number of times the letter appear in the string( i.e not printing everything from a-z .. but just the number of time 'a' appeared etc)
0
 char ch= 97

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

1 Comment

if you use"ch=ch+3" , it'll print 'd'.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.