In this program it is taking in a number, translating it to letters, and then pr
ID: 664859 • Letter: I
Question
In this program it is taking in a number, translating it to letters, and then printing out how many of each of the letters in the alphabet are used for that word. This is the part of the program that is to count the letters. Can someone please explain, in detail, how this is code is supposed to work?
int number;
while (string[number] != '')
{
if (string[number] >= 'a' && string[number] <= 'z')
count[string[number]-'a']++;
number++;
}
for (number = 0; number < 26; number++)
{
if (count[c] != 0)
printf("%c occurs %d times in the entered string. ",c+'a',count[c]);
}
Explanation / Answer
If you test yourselfhe ordinal of a is 97 Each lower case letter is in the range 97-122 (26 characters.) So, if you just subtract 96 from the ordinal of any lower case letter, you will get its position in the alphabet assuming you take 'a' == So, ordinal of 'b' == 98, 'c' == 99, etc. When you subtract 96, 'b' == 2, 'c' == 3, etc.
Here so can see data number define as Integer .First condition check where string is empty or not.then string is not empty condition is true.then go to next
if (string[number] >= 'a' && string[number] <= 'z')
Check the range whether String value in range or not ..if condition have same range count the word otherwise not.here testing value and count .
count [str.charAt(i)- 'a']++;
i++;
}
for (i =0 ;i <26; i++)
{
count =count+i;
}
str.substring(count);
System.out.println(ch);
System.out.println(count);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.