Knowee
Questions
Features
Study Tools

A substring is a contiguous sequence of characters within a string. Given a string S and an integer n, write a C code to generate all substrings of length n without vowels. For example, if the given string S1 is ‘apple’ then substrings of S1 of length 3 are app, ppl, pple. And substrings of length 3 without vowels is only one and it is ppl.

Question

A substring is a contiguous sequence of characters within a string. Given a string S and an integer n, write a C code to generate all substrings of length n without vowels. For example, if the given string S1 is ‘apple’ then substrings of S1 of length 3 are app, ppl, pple. And substrings of length 3 without vowels is only one and it is ppl.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple C code to generate all substrings of length n without vowels from a given string.

#include <stdio.h>
#include <string.h>

// Function to check if a character is vowel
int isVowel(char c) {
    c = tolower(c);
    return (c=='a' || c=='e' || c=='i' || c=='o' || c=='u');
}

// Function to generate all substrings of length n without vowels
void generateSubstrings(char *str, int n) {
    int len = strlen(str);
    for (int i = 0; i <= len - n; i++) {
        int flag = 0;
        for (int j = i; j < i + n; j++) {
            if (isVowel(str[j])) {
                flag = 1;
                break;
            }
        }
        if (flag == 0) {
            for (int j = i; j < i + n; j++)
                printf("%c", str[j]);
            printf("\n");
        }
    }
}

int main() {
    char str[] = "apple";
    int n = 3;
    generateSubstrings(str, n);
    return 0;
}

This program works as follows:

  1. The isVowel function checks if a character is a vowel.
  2. The generateSubstrings function generates all substrings of length n. For each substring, it checks if it contains a vowel. If it does, it skips to the next substring. If it doesn't, it prints the substring.
  3. In the main function, we define the string and the length of the substrings, and call the generateSubstrings function.

This problem has been solved

Similar Questions

Write a program to find the substring of a given string.

What is the relationship between a text string and a substring?

Write a function:class Solution { public int solution(String S); }that, given a string S consisting of N lowercase English letters, returns the length of the longest substring in which every letter occurs an even number of times. A substring is defined as a contiguous segment of a string. If no such substring exists, return 0.Examples:1. Given S = "bdaaadadb", the function should return 6. Substrings in which every letter occurs an even number of times are "aa", "adad", "daaada" and "aaadad". The length of the longest of them is 6.2. Given S = "abacb", the function should return 0. There is no non-empty substring in which every letter occurs an even number of times.3. Given S = "zthtzh", the function should return 6. Every letter in the whole string occurs an even number of times.Write an efficient algorithm for the following assumptions:N is an integer within the range [1..100,000];string S consists only of lowercase letters ('a'-'z').

Longest Substring Without Repeating Characters

Given a string S consisting of only lowercase characters, find the length of the longest substring that does not contain any vowel.Input FormatFirst line of input contains T - number of test cases. Its followed by T lines, each line contains a string of size N, containing only lowercase english alphabets.Output FormatFor each test case, print the length of the largest substring that does not contain any vowel, separated by newline.Constraints1 <= T <= 1001 <= N <= 104

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.