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
Tcp takes a three-step approach to establishing a reliable communication. first, from the transport layer of the sending device
tiny-mole [99]

TCP takes a three-step approach to establishing a reliable communication.

First, from the transport layer of the sending device a request packet is transmitted to the receiving device's transport layer asking if a session can be established.

Second, if available, the receiving device sends a packet back to the sending device indicating that it is available for communication.

Third, the sending device begins to send its data to the receiving device.

4 0
2 years ago
Name and define (or list the set that defines) three of the four common data types in programming
aliina [53]

In Computer programming, there are four (4) common data types and these include:

  1. Integer type (int):
  2. Floating point type (float)
  3. Boolean (bool)
  4. String (str)

<h3>The kinds of data type.</h3>

In Computer programming, there are four (4) common data types and these include:

  • <u>Integer type (int):</u> it is used for storing whole numbers such as 1, 2, 3, 4, ..... 2,147,483,647.

  • <u>Floating point type (float):</u> it is used for defining real numbers that have a decimal point such as 0.009, 9.87, 5.6, etc.

  • <u>Boolean (bool):</u> it allow users to combine keywords with Boolean operators (quotes, AND, OR, near/n) as either true or false.

  • <u>String (str):</u> it is used for data values that are made up of ordered sequences of characters.

Read more on a data types here: brainly.com/question/25619349

3 0
2 years ago
Fill in the blanks to complete a summary of this part of the passage. For the power of Patents
PIT_PIT [208]
I’m confused . what do i do?
6 0
2 years ago
Read 2 more answers
3.A ball is thrown into the air with an initial velocity of 15 m/s. a.How long does it take the ball to reach maximum height?b.W
ehidna [41]

Answer:  a) = 1.5 sec.  b= 11.5 m. c=0  d= 1.5 sec.

Explanation:

a) Once thrown into the air , the only influence on the ball is gravity, so, we can conclude that the acceleration acting on the ball is the one due to gravity, i.e., g= 9.8 m/sec².

Applying the definition of acceleration, we can write the following equation:

a= (vf-vo) / t   ⇒ g = vf-vo / t. (1)

Now, when the ball reaches to his maximum height, it stops momentarily, and then starts to fall, so for this height, we can write vf=0.

Replacing  in (1), and solving for t, we get:

t = (0 -vo) / g = -15 m/s/ - 9.8 m/sec² (Taking upward as positive  direction)

a) t= 1.5 sec.

Replacing this value in the kinematic equation for displacement, when vo=15 m/s, we get:

hmax: 15 m/sec . 1.5 sec -1/2. 9.8 m/sec². (1.5 sec) ² (2)

b) hmax= 11.5 m

c) vf= 0 (as explained above)

Finally, in order to know how long the ball takes to return to its initial height, we know that for the falling part of the trajectory, vo=0, so we can write the equation for displacement as follows:

h=-hmax (because it returns to the same starting point)= 1/2 gt²=-11.5 m

Solving for t, we get:

d) t=1.5 sec.

It can be seen that the time used in going up, it is the same for going down, to travel the same distance.

This result is completely general and applies to any free fall problem.

3 0
3 years ago
What is the full form of bcc please tell​
expeople1 [14]

Blind Carbon Copy

For emails. It is used to send a copy of the email to somebody without the original person receiving it knowing that you sent a copy.

3 0
2 years ago
Read 2 more answers
Other questions:
  • Thomas Hill claims that a fruitful way to think about the badness of destroying the environment is
    15·1 answer
  • Which component is the smallest unit in a spreadsheet?
    14·2 answers
  • Principles of defensive driving include:
    15·1 answer
  • A computer’s memory is composed of 8K words of 32 bits each. How many bits are required for memory addressing if the smallest ad
    10·1 answer
  • Binary Numbers and Conversion 1. Complete the following decimal-to-binary number conversions. a) 17 (10) b) 34 (10) c) 58 (10) d
    9·1 answer
  • Okay, guys, I know this one will be very hard however while trying to finish this assignment I got overwhelmed and lost by the s
    9·1 answer
  • ______allow you to select elements that are in a certain state, such as when the mouse if hovering over an element
    14·1 answer
  • Write a program to simulate a Fruit Machine that displays three symbols at random from Cherry, Bell, Lemon, Orange, Star, Skull.
    11·1 answer
  • Predict the output
    7·1 answer
  • ASAP 50 POINTS
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!