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]
3 years ago
13

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

Computers and Technology
1 answer:
stiv31 [10]3 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
Var1 = 1<br> var2 = 2<br> var3 = "3"<br> print(var1 + var2 + var3)
Savatey [412]

Explanation:

omg it's Python, I don't like it

I would say the answer is 33 or 6

5 0
3 years ago
At Greenwood ATCT, arrival information need NOT be forwarded while FDIO is operational unless the sequence of aircraft changes a
GuDViN [60]

The available options are:

A. Arrival time differs by more than 3 minutes

B. Aircraft is issued an approach other than the tower specified

C. Verbal coordination has not been yet accomplished

Answer:

Aircraft is issued an approach other than the tower specified

Explanation:

Considering the situation described in the question, and according to Instrument Flight Rules (IFR), Approach Clearance Procedures, when at Greenwood Air Traffic Control Tower (ACTC), arrival information need not be forwarded while Flight Data Input-Output (FDIO) operational unless the sequence of aircraft changes and the "Aircraft is issued an approach other than the tower specified."

3 0
2 years ago
Having a good credit score is important because:
Lisa [10]
It would be better if we could see the options, fully. But from what I can see, it should be B.
7 0
3 years ago
As Alexa types a message, Outlook autosaves the message at various points. In which folder is the message located if Alexa wants
alexgriva [62]

Answer:

Draft

Explanation:

Draft is a specific folder in email that is meant to store partially finished mails and mails which has been created but is not sent .  Draft folders helps in reducing rework on the mail if one is interrupted while writing the mail. Another purpose of draft folder was that if anyone accidentally closes the  mail writing section then the content of mail is not lost and can be easily be obtained in draft folder. It also helps in tracking unsent and unfinished mail for the user.

Since, message written by Alexa is saved at different points but is not sent then if the mail is to be sent, then one needs to look for that mail in Draft folder.

5 0
3 years ago
Read 2 more answers
Select the best answer for the question. 2. What is the simplest way to permanently get rid of an unwanted file?
Kobotan [32]
To permanetly get rid of an unwanted file, you delete it
3 0
3 years ago
Other questions:
  • What file would you edit to restrict the number of simultaneous logins a user can employ??
    14·1 answer
  • Use the following global structure declaration as an example only. You will need to create your own data structure based on a da
    5·1 answer
  • Complete method printPopcornTime(), with int parameter bagOunces, and void return type. If bagOunces is less than 2, print "Too
    5·1 answer
  • Explain the purpose of QoS on a TCP/IP network. Define the basic purpose of IP prece- dence, Type of Service, Diffserv, and Expl
    11·1 answer
  • Raul in Colombia can enter data into a spreadsheet. Olivia in England can access the spreadsheet a few minutes later and use Rau
    7·1 answer
  • Users of Adobe Reader, created by Adobe Systems Incorporated, are prompted to provide feedback on their experiences with the sof
    7·1 answer
  • Which of the following is NOT a valid name for a function?
    5·1 answer
  • What is a text based language that can be used to build all kinds of things ????
    9·1 answer
  • PLEASE SOMEONE ANSWER THIS
    14·2 answers
  • What is computer assisted translation​
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!