Day 9 of Programming in C

Write a program to count no of vowels and consonants in a string.


#include <stdio.h>
void main()
{
    int vowels,consonants,i;
    i = vowels = consonants = 0;
    char str[100];
    printf("Enter the string : ");
    gets(str);
    while(str[i]!='\0'){
        if(str[i]==' '){
            i++;
            continue;
        }
        switch (str[i]) {
            case 'a': case 'e': case 'i': case 'o': case 'u':
            case 'A': case 'E': case 'I': case 'O': case 'U':
                vowels++;
                break;
            default:
            if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z')){
            consonants++;
             }
        }
     i++;
    }
    printf("No of Vowels = %d\nNo of Consonants = %d",vowels,consonants);
}
    
    
Step 1:

Include the `stdio.h` header file.

  • This header provides standard input/output functions, allowing the program to interact with the user via `printf` (for output) and `gets` (for input).

Step 2:

Define the `main` function.

  • This is the entry point of the program.

Step 3:

Declare integer variables `vowels`, `consonants`, and `i`.

  • `vowels` and `consonants` will store the counts of vowels and consonants, respectively.

  • `i` will be used as an index to iterate through the string.

Step 4:

Initialize `i`, `vowels`, and `consonants` to 0.

Step 5:

Declare a character array `str` of size 100 to store the input string.

Step 6:

Prompt the user to enter a string using `printf`.

Step 7:

Read the input string using `gets(str)`.

Step 8:

Enter a `while` loop that continues as long as the current character `str[i]` is not the null terminator `\0`.

Step 9:

Inside the loop, check if the current character `str[i]` is a space.

  • If it's a space, increment `i` and use `continue` to skip to the next iteration of the loop.

Step 10:

Use a `switch` statement to check if the current character is a vowel (lowercase or uppercase).

  • If it's a vowel, increment `vowels` and use `break` to exit the `switch` statement.

Step 11:

If the current character is not a vowel, check if it's an alphabet (lowercase or uppercase).

  • If it's an alphabet, increment `consonants`.

Step 12:

Increment `i` to move to the next character in the string.

Step 13:

The `while` loop continues until the end of the string is reached.

Step 14:

Print the counts of vowels and consonants using `printf`.

Step 15:

The program execution ends.

Write a program to count no of words in a string.


#include <stdio.h>
void main()
{
    int words = 1, i = 0;
    char str[100];
    printf("Enter the string : ");
    gets(str);
    while(str[i] != '\0'){
       if(str[i] == ' '){
         words++;
       }
       i++;
    }
    printf("No of words = %d",words);
}

        
    
    
Step 1:

Include the `stdio.h` header file.

  • This header provides standard input/output functions, allowing the program to interact with the user via `printf` (for output) and `gets` (for input).

Step 2:

Define the `main` function.

  • This is the entry point of the program.

Step 3:

Declare integer variables `words` and `i`.

  • `words` will store the count of words in the string.

  • `i` will be used as an index to iterate through the string.

Step 4:

Initialize `words` to 1 and `i` to 0.

  • `words` is initialized to 1 because even a single word string will have at least one word.

Step 5:

Declare a character array `str` of size 100 to store the input string.

Step 6:

Prompt the user to enter a string using `printf`.

Step 7:

Read the input string using `gets(str)`.

Step 8:

Enter a `while` loop that continues as long as the current character `str[i]` is not the null terminator `\0`.

Step 9:

Inside the loop, check if the current character `str[i]` is a space.

  • If it's a space, increment `words`.

  • This assumes that words are separated by single spaces.

Step 10:

Increment `i` to move to the next character in the string.

Step 11:

The `while` loop continues until the end of the string is reached.

Step 12:

Print the count of words using `printf`.

Step 13:

The program execution ends.

Write a program to find the no of occurences of each unique character and its frequency.


#include <stdio.h>
void main()
{
    char str[100];
    int freq[256] = {0};
    printf("Enter the string : ");
    gets(str);
    for(int i=0;str[i]!='\0';i++){
        freq[(int)str[i]]++;
    }
    for(int i=0;i<=255;i++){
        if(freq[i]>0)
        printf("occurence of %c is %d times\n",i,freq[i]);
    }
}

        
    
Step 1:

Include the `stdio.h` header file.

  • This header provides standard input/output functions, allowing the program to interact with the user via `printf` (for output) and `gets` (for input).

Step 2:

Define the `main` function.

  • This is the entry point of the program.

Step 3:

Declare a character array `str` of size 100 to store the input string.

Step 4:

Declare an integer array `freq` of size 256, initialized to 0.

  • This array will store the frequency of each character (ASCII value 0-255).

Step 5:

Prompt the user to enter a string using `printf`.

Step 6:

Read the input string using `gets(str)`.

Step 7:

Enter a `for` loop to iterate through the string.

  • The loop continues as long as the current character `str[i]` is not the null terminator `\0`.

Step 8:

Inside the loop, increment the frequency count for the current character.

  • `freq[(int)str[i]]++;` converts the character to its ASCII integer value and increments the corresponding element in the `freq` array.

Step 9:

Enter another `for` loop to iterate through the `freq` array (from 0 to 255).

Step 10:

Inside the loop, check if the frequency count for the current character is greater than 0.

  • `if (freq[i] > 0)` checks if the character appeared in the string.

Step 11:

If the frequency count is greater than 0, print the character and its frequency using `printf`.

  • `printf("occurence of %c is %d times\n", i, freq[i]);` prints the character (converted back from ASCII to char) and its count.

Step 12:

The program execution ends.

Write a program to remove all repetitive characters from a string.


#include <stdio.h>
void main()
{
    char str[100], temp[100];
    int freq[256] = {0};
    int i, j = 0;

    printf("Enter the string: ");
    gets(str);

    for (i = 0; str[i] != '\0'; i++) {
        if (freq[(int)str[i]] == 0) {
            freq[(int)str[i]] = 1;
            temp[j++] = str[i];
        }
    }
    temp[j] = '\0';

    printf("String after removing duplicates: %s", temp);
}
  
 
    
Step 1:

Include the `stdio.h` header file.

  • This header provides standard input/output functions, allowing the program to interact with the user via `printf` (for output) and `gets` (for input).

Step 2:

Define the `main` function.

  • This is the entry point of the program.

Step 3:

Declare character arrays `str` and `temp`, each of size 100.

  • `str` will store the input string.

  • `temp` will store the string with duplicates removed.

Step 4:

Declare an integer array `freq` of size 256, initialized to 0.

  • This array will act as a flag to track whether a character has already been encountered.

Step 5:

Declare integer variables `i` and `j`, initializing `j` to 0.

  • `i` will be used to iterate through the input string.

  • `j` will be used to index the `temp` array.

Step 6:

Prompt the user to enter a string using `printf`.

Step 7:

Read the input string using `gets(str)`.

Step 8:

Enter a `for` loop to iterate through the input string.

  • The loop continues as long as the current character `str[i]` is not the null terminator `\0`.

Step 9:

Inside the loop, check if the current character has already been encountered.

  • `if (freq[(int)str[i]] == 0)` checks if the flag for the current character is 0 (not encountered).

Step 10:

If the character has not been encountered, set its flag to 1 and add it to the `temp` array.

  • `freq[(int)str[i]] = 1;` sets the flag to 1, indicating the character has been encountered.

  • `temp[j++] = str[i];` adds the character to the `temp` array and increments `j`.

Step 11:

After the loop, add the null terminator to the `temp` array.

  • `temp[j] = '\0';` marks the end of the string in `temp`.

Step 12:

Print the string with duplicates removed using `printf`.

  • `printf("String after removing duplicates: %s", temp);` displays the contents of the `temp` array.

Step 13:

The program execution ends.

Write a program to search a particular substring in a given string.


#include <stdio.h>
void main()
{
    char str[100], substr[100];
    int i, j, found=0;
    printf("Enter the string: ");
    gets(str);
    printf("Enter the sub-string: ");
    gets(substr);
    for (i = 0; str[i] != '\0'; i++)
    {
        found = 1;
        for (j = 0; substr[j] != '\0'; j++)
        {
            if (str[i + j] != substr[j])
            {
                found = 0;
                break;
            }
        }
        if(found){
            printf("Substring found at index %d",i);
            return;
        }
    }
    printf("Substring not found");
}
  
 
    
Step 1:

Include the `stdio.h` header file.

  • This header provides standard input/output functions, allowing the program to interact with the user via `printf` (for output) and `gets` (for input).

Step 2:

Define the `main` function.

  • This is the entry point of the program.

Step 3:

Declare character arrays `str` and `substr`, each of size 100.

  • `str` will store the main string.

  • `substr` will store the substring to search for.

Step 4:

Declare integer variables `i`, `j`, and `found`, initializing `found` to 0.

  • `i` will be used to iterate through the main string.

  • `j` will be used to iterate through the substring.

  • `found` will act as a flag to indicate if the substring is found.

Step 5:

Prompt the user to enter the main string using `printf`.

Step 6:

Read the main string using `gets(str)`.

Step 7:

Prompt the user to enter the substring using `printf`.

Step 8:

Read the substring using `gets(substr)`.

Step 9:

Enter a `for` loop to iterate through the main string.

  • The loop continues as long as the current character `str[i]` is not the null terminator `\0`.

Step 10:

Inside the outer loop, set `found` to 1, assuming the substring might be found at the current position.

Step 11:

Enter an inner `for` loop to iterate through the substring.

  • The loop continues as long as the current character `substr[j]` is not the null terminator `\0`.

Step 12:

Inside the inner loop, compare the current character of the main string (starting from `i`) with the current character of the substring.

  • `if (str[i + j] != substr[j])` checks if the characters are different.

Step 13:

If the characters are different, set `found` to 0 and break out of the inner loop.

Step 14:

After the inner loop, check if `found` is still 1.

  • `if (found)` checks if the substring was found at the current position.

Step 15:

If `found` is 1, print the index `i` where the substring was found and use `return` to exit the `main` function.

Step 16:

If the outer loop completes without finding the substring, print a message indicating that the substring was not found.

Step 17:

The program execution ends.

Write a program to remove all the spaces from given string.


#include <stdio.h>
void main()
{
    void main() {
    char str[100],result[100];
    int i = 0, j = 0;
    printf("Enter the string: ");
    gets(str);

    while (str[i] != '\0') {
        if (str[i] != ' ') {
            result[j++] = str[i];
        }
        i++;
    }
    result[j] = '\0';

    printf("String after removing spaces: %s", result);
}
  
 
    
Step 1:

Include the `stdio.h` header file.

  • This header provides standard input/output functions, allowing the program to interact with the user via `printf` (for output) and `gets` (for input).

Step 2:

Define the `main` function.

  • This is the entry point of the program.

Step 3:

Declare character arrays `str` and `result`, each of size 100.

  • `str` will store the input string.

  • `result` will store the string with spaces removed.

Step 4:

Declare integer variables `i` and `j`, initializing both to 0.

  • `i` will be used to iterate through the input string.

  • `j` will be used to index the `result` array.

Step 5:

Prompt the user to enter a string using `printf`.

Step 6:

Read the input string using `gets(str)`.

Step 7:

Enter a `while` loop that continues as long as the current character `str[i]` is not the null terminator `\0`.

Step 8:

Inside the loop, check if the current character `str[i]` is not a space.

  • `if (str[i] != ' ')` checks if the character is not a space.

Step 9:

If the character is not a space, add it to the `result` array and increment `j`.

  • `result[j++] = str[i];` adds the character to `result` and increments `j`.

Step 10:

Increment `i` to move to the next character in the input string.

Step 11:

After the loop, add the null terminator to the `result` array.

  • `result[j] = '\0';` marks the end of the string in `result`.

Step 12:

Print the string with spaces removed using `printf`.

  • `printf("String after removing spaces: %s", result);` displays the contents of the `result` array.

Step 13:

The program execution ends.

Step 14:

The code provided has a nested main function, which is a syntax error in standard C. Only one main function should exist.

Display the list of all characters of the string "+,-,%" with their ASCII value in a tabular form.


#include <stdio.h>
void main()
{
    void main() {
    char str[] = "+,-,%";
    int i = 0;
    printf("Character\tASCII Value\n");
    while (str[i] != '\0') {
        printf("   %c\t\t   %d\n", str[i],str[i]);
        i++;
    }
}
  
 
    
Step 1:

Include the `stdio.h` header file.

  • This header provides standard input/output functions, allowing the program to interact with the user via `printf` (for output).

Step 2:

Define the `main` function.

  • This is the entry point of the program.

Step 3:

Declare a character array `str` and initialize it with the string "+,-,%".

Step 4:

Declare an integer variable `i` and initialize it to 0.

  • `i` will be used as an index to iterate through the string.

Step 5:

Print the header for the output table using `printf`.

  • `printf("Character\tASCII Value\n");` prints the column headers "Character" and "ASCII Value" with a tab separator.

Step 6:

Enter a `while` loop that continues as long as the current character `str[i]` is not the null terminator `\0`.

Step 7:

Inside the loop, print the current character and its ASCII value using `printf`.

  • `printf(" %c\t\t %d\n", str[i], str[i]);` prints the character `str[i]`, a tab, and its ASCII value (which is implicitly converted to an integer).

  • The extra spaces and tabs are used to align the output in a table format.

Step 8:

Increment `i` to move to the next character in the string.

Step 9:

The `while` loop continues until the end of the string is reached.

Step 10:

The program execution ends.

Step 11:

The code provided has a nested main function, which is a syntax error in standard C. Only one main function should exist.

Comments

Popular Posts