Question

In: Computer Science

The following program should swap the vowels between two strings as long as the end of...

The following program should swap the vowels between two strings as long as the end of either string has not been reached.

Example
Enter the first string: james
Enter the second string: naomi
jamos
naemi

Complete the program by finishing the swap_vowels function. You may not modify ANY existing code given.

#include 
#include 

#define MAX_SIZE 128

int isvowel(char c) {
    switch (c) {
        case 'a':
        case 'A':
        case 'e':
        case 'E':
        case 'i':
        case 'I':
        case 'o':
        case 'O':
        case 'u':
        case 'U':
            return 1;
    }
    return 0;
}

void swap_vowels(char str1[], char str2[]) {
    int i = 0, j = 0;

    while (str1[i] != 0 && str2[j] != 0) {
        // Skip to the next vowel in str1
        while (str1[i] != 0 && !isvowel(str1[i])) {
            i++;
        }

        // Skip to the next vowel in str2
        while (str2[j] != 0 && !isvowel(str2[j])) {
            j++;
        }

        // Write your code here
    }
}

int main() {
    char str1[MAX_SIZE] = { 0 };
    char str2[MAX_SIZE] = { 0 };

    printf("Enter the first string: ");
    fgets(str1, MAX_SIZE, stdin);
    str1[strlen(str1)-1] = 0;

    printf("Enter the second string: ");
    fgets(str2, MAX_SIZE, stdin);
    str2[strlen(str2)-1] = 0;

    // Swap the vowels
    swap_vowels(str1, str2);
    printf("%s\n%s\n", str1, str2);

    return 0;
} 

Need it in 10 minutes, please.

Solutions

Expert Solution

Code:

#include <bits/stdc++.h>

#define MAX_SIZE 128

int isvowel(char c) {
    switch (c) {
        case 'a':
        case 'A':
        case 'e':
        case 'E':
        case 'i':
        case 'I':
        case 'o':
        case 'O':
        case 'u':
        case 'U':
            return 1;
    }
    return 0;
}

void swap_vowels(char str1[], char str2[]) {
    int i = 0, j = 0;

    while (str1[i] != 0 && str2[j] != 0) {
        // Skip to the next vowel in str1
        while (str1[i] != 0 && !isvowel(str1[i])) {
            i++;
        }

        // Skip to the next vowel in str2
        while (str2[j] != 0 && !isvowel(str2[j])) {
            j++;
        }
        
        // Write your code here
        
        // end of either of the string
                if(str1[i]==0)  break;
                if(str2[j]==0) break;
                
                // if valid indexes then swap using temp
        char temp = str1[i];
        str1[i] = str2[j];
        str2[j] = temp;
        i++;
        j++;
    }
}

int main() {
    char str1[MAX_SIZE] = { 0 };
    char str2[MAX_SIZE] = { 0 };

    printf("Enter the first string: ");
    fgets(str1, MAX_SIZE, stdin);
    str1[strlen(str1)-1] = 0;

    printf("Enter the second string: ");
    fgets(str2, MAX_SIZE, stdin);
    str2[strlen(str2)-1] = 0;

    // Swap the vowels
    swap_vowels(str1, str2);
    printf("%s\n%s\n", str1, str2);

    return 0;
} 

Code Screenshot:

Code output:

Explanation:

If either of strings has ended then we break out. This is the first condition here.

Then otherwise we swap the i of str1 and j of str2 and increment them. Then again this loop continues.

======================

For any query comment.


Related Solutions

Program Zeus:    Complete the Count Vowels program (Count Vowels.cpp template provided at the end) to include two...
Program Zeus:    Complete the Count Vowels program (Count Vowels.cpp template provided at the end) to include two user defined functions (which you will write!). Code lenguage is C++ bool isVowel (char c)       // returns true if c is a vowel and false otherwise int countVowels (string s) // returns the number of vowels in s. You can access each character of s by using s.at(i) where i ranges from 0 to s.length()-1. countVowels () should call isVowel (s.at(i)) to check...
MUST BE DONE IN C (NOT C++) This program should utilize the basics of strings. First,...
MUST BE DONE IN C (NOT C++) This program should utilize the basics of strings. First, declare and initialize a string. You can name the variable whichever way you want and you can initialize it to whichever value you want. Then, use a for loop to print its characters; one at a time, until you reach the null character. After this, go ahead and declare a second string (since you are not initializing it right away, you will have to...
Write a C++ program to swap two numbers and show your output
Write a C++ program to swap two numbers and show your output
This program works with strings complete the following functionsto allow the program to function properly;...
This program works with strings complete the following functions to allow the program to function properly;a) Write a C function int string_to_number(char *) that takes a string of decimal digits as an input parameter, and returns the integer value that the string represents. For example, if the char s[] ="256", then string_to_number(s) returns integer 256 (int data type). You are allowed to use strlen(…) function, but not allowed to use atoi and other functions. Ignore the negative sign and overall.b)...
Write a JAVA program that compares two strings input to see if they are the same?
Write a JAVA program that compares two strings input to see if they are the same?
Working with Strings The following program illustrates the use of some of the methods in the...
Working with Strings The following program illustrates the use of some of the methods in the String class. Study the program to see what it is doing. // *************************************************************** // StringManips.java // Test several methods for manipulating String objects // *************************************************************** import java.util.Scanner; public class StringManips { public static void main (String[] args) { String phrase = new String ("This is a String test."); int phraseLength; // number of characters in the phrase String int middleIndex; // index of the...
Use MIPS assembly language program to swap two of the integers in an integer array. The...
Use MIPS assembly language program to swap two of the integers in an integer array. The program should include the Swap function to swap the integers and the main function to call the Swap function. The main function should: • Pass the starting address of the array in $a0. • Pass the indices of the two elements to swap in $a1 and $a2. • Preserve (i.e. push onto the stack) any T registers that it uses. • Call the Swap...
Standing waves are set up on two strings fixed at each end, as shown in the...
Standing waves are set up on two strings fixed at each end, as shown in the drawing. The two strings have the same tension and mass per unit length, but they differ in length by 0.53 cm. The waves on the shorter string propagate with a speed of 41.2 m/s, and the fundamental frequency of the shorter string is 227 Hz. Determine the beat frequency produced by the two standing waves.
Standing waves are set up on two strings fixed at each end, as shown in the...
Standing waves are set up on two strings fixed at each end, as shown in the drawing. The two strings have the same tension and mass per unit length, but they differ in length by 0.54 cm. The waves on the shorter string propagate with a speed of 42.3 m/s, and the fundamental frequency of the shorter string is 239 Hz. Determine the beat frequency produced by the two standing waves.
8 IP Two strings that are fixed at each end are identical, except that one is...
8 IP Two strings that are fixed at each end are identical, except that one is 0.530 cm longer than the other. Waves on these strings propagate with a speed of 34.2 m/s , and the fundamental frequency of the shorter string is 217 Hz . Part A What beat frequency is produced if each string is vibrating with its fundamental frequency? fbeat = nothing Hz Request Answer Part B Does the beat frequency in part (a) increase or decrease...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT