1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
sineoko [7]
2 years ago
13

How to write a function that counts the letters in a string in C?

Computers and Technology
1 answer:
stiv31 [10]2 years ago
3 0

Answer:

Here is the C function that counts the letters in a string:

int LetterCount(char string[]){  //function to count letters in a string passed as parameter

 string[100];  // char type string with size 100

  int i, letters;  // letter variable stores the count for no. of letters in string

   i = letters = 0;  //initialize variables i and letters to 0

  while (string[i] != '\0')  { // loop iterates through the entire string until end of string is reached

    if( (string[i] >= 'a' && string[i] <= 'z') || (string[i] >= 'A' && string[i] <= 'Z') )  { // if condition checks the letters in the string

     letters++;    }  // increments 1 to the count of letters variable each time a letter is found in the string

    i++;  }  //increments value of i to move one character forward in string

   printf("Number of Letters in this String = %d", letters);   // displays the number of letters in the string

   return 0; }                              

Explanation:

Here the question means that the function should count the letters in a string. So the letters are the alphabets from a to z and A to Z.

Here is the complete program:

#include <stdio.h>   // to use input output functions

int LetterCount(char string[]){  // method that takes a character string as parameter and counts the letters in the string

string[100];  

int i, letters;

i = letters = 0;

while (string[i] != '\0')   {

 if( (string[i] >= 'a' && string[i] <= 'z') || (string[i] >= 'A' && string[i] <= 'Z'))

   {letters++;  }

   i++; }

   printf("Number of Alphabets in this String = %d", letters);  

   return 0;}  

int main(){  // start of main function

  char string[100];  //declares a char array of string

   printf("Please Enter a String : ");   //prompts user to enter a string

  fgets(string,100,stdin);  //get the input string from user

   LetterCount(string); } // calls method to count letters in input string

I will explain this function with an example

Lets suppose the user input "abc3" string

string[100] = "abc3"

Now the function has a while loop that while (string[i] != '\0')  that checks if string character at i-th position is not '\0' which represents the end of the character string. As value of i = 0 so this means i is positioned at the first character of array i.e. 'a'

At first iteration:

i = 0

letter = 0

if( (string[i] >= 'a' && string[i] <= 'z') || (string[i] >= 'A' && string[i] <= 'Z') )   if condition checks if the character at i-th position of string is a letter. As the character at 0-th position of string is 'a' which is a letter so this condition evaluates to true. So the statement letter++ inside if condition executes which increments the letter variable to 1. So the value of letter becomes 1. Next statement i++ increments the value of i to 1. So i becomes 1. Hence:

i = 1

letter = 1

At second iteration:

i = 1

letter = 1

if( (string[i] >= 'a' && string[i] <= 'z') || (string[i] >= 'A' && string[i] <= 'Z') )  

if condition checks if the character at i-th position of string is a letter. As the character at 1st position of string is 'b' which is a letter so this condition evaluates to true. So the statements letter++ inside if condition and i++ executes which increments these variables to 1. Hence:

i = 2

letter = 2

At third iteration:

i = 2

letter = 2

if( (string[i] >= 'a' && string[i] <= 'z') || (string[i] >= 'A' && string[i] <= 'Z') )  

if condition checks if the character at i-th position of string is a letter. As the character at 2nd position of string is 'c' which is a letter so this condition evaluates to true. So the statements letter++ inside if condition and i++ executes which increments these variables to 1. Hence:

i = 3

letter = 3

At fourth iteration:

i = 3

letter = 3

if( (string[i] >= 'a' && string[i] <= 'z') || (string[i] >= 'A' && string[i] <= 'Z') )  

if condition checks if the character at i-th position of string is a letter. As the character at 3rd position of string is '3' which is not a letter but a digit so this condition evaluates to false. So the statement letter++ inside if condition does not execute. Now i++ executes which increments this variable to 1. Hence:

i = 4

letter = 3

Now the loop breaks because while (string[i] != '\0') condition valuates to false as it reaches the end of the string.

So the statement: printf("\n Number of Letters in this String = %d", letters); executes which prints the value of letters on the output screen. Hence the output is:

Number of Letters in this String = 3

You might be interested in
Blending two or more colours is called​
krek1111 [17]

Answer:

shading?

Explanation:

Ombré /ˈɒmbreɪ/ (literally "shaded" in French) is the blending of one color hue to another, usually moving tints and shades from light to dark. It has become a popular feature for hair coloring, nail art, and even baking, in addition to its uses in home decorating and graphic design.

3 0
2 years ago
Write a python program to check whether the number is divisible by 7. If its divisible, print its divisible otherwise find previ
Stells [14]

f = int(input("Enter a number: "))

if(f%7==0):

   print("It's divisible by 7.")

else:

   if(f<7):

       print("7")

   else:

       print(str(f-(f%7))+", "+str(f+(7-(f%7))))

7 0
1 year ago
Create a horizontal line across middle of the entire form. To code this the ____ values are the same.
fredd [130]

To make a horizontal line in mathematics you require some sort of constant function. Where no matter the input x the output y will always equal to a certain number n.

For eg. consider n = 2. Hence the graph of line has an equation of y = n making it always intercept in (x, n). As x increases the y is the same therefore the horizontal infinite line emerges.

So to plot or code this kind of program you need to have the same values of y therefore y is a constant function.

Hope this helps.

r3t40

5 0
3 years ago
Read 2 more answers
1. Write a generic method that compares its 2 arguments using the equals method and returns true if they are equal and false oth
Ganezh [65]

Answer:

Explanation:

The following piece of code is written in Java. It creates the method as requested that takes in two generic objects and compares them using the .equals() built in Java method. This method will return True if the objects are identical or False if they are not. A test case is used in the code and the output can be seen in the attached image below.

   public static <T> boolean comparePerez(T a, T b) {

       return a.equals(b);

   }

5 0
3 years ago
Write a program with total change amount as an integer input that outputs the change using the fewest coins, one coin type per l
alexandr1967 [171]

Answer:

a=input("Amount in pennies")

b=int(a)

dollars=0

dimes= 0

quarters=0

nickels=0

pennies = 0

dollars = int(b/100)

b= b- dollars *100

quarters=int(b/25)

b=b-quarters*25

dimes = int(b/10)

b = b -dimes*10

nickels=int(b/5)

b=b - nickels * 5

pennies = b

print(dollars)

print(dimes)

print(nickels)

print(pennies)

Explanation:

The required program is in answer section. Note, the amount is entered in pennies.

7 0
3 years ago
Other questions:
  • Finding your style in photography is about ___
    10·2 answers
  • If you are logged on to Windows Live Messenger, why would you be unable to engage
    6·1 answer
  • The degree to which a firewall can impose user access restrictions is known as which of the following?Security assurancePrivileg
    8·1 answer
  • A user has just reported that he downloaded a file from a prospective client using IM. The user indicates that the file was call
    11·1 answer
  • Which network component blocks status?
    9·1 answer
  • So I was looking at my profile and it says i'm a brainly PLUS member didn't sign up for it tho. Any ideas why this could have ha
    14·2 answers
  • You attempt to telnet to system 192.168.1.240. You receive the following message: "Connecting To 192.168.1.240...Could not open
    5·1 answer
  • Enthusiasm codehs 7.6.4 Write a function called add_enthusiasm that takes a string and returns that string in all uppercase with
    15·1 answer
  • Is there a way for me to play a .mp3 file from my computer using Atom?
    10·1 answer
  • ....................................................................................................
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!