Question

In: Computer Science

C++ PROGRAM (Pointers and char arrays) IMPORTANT NOTES: 1. CHAR ARRAY MUST BE USED. 2. YOU...

C++ PROGRAM (Pointers and char arrays)

IMPORTANT NOTES:

1. CHAR ARRAY MUST BE USED.

2. YOU MUST USE POINTERS.

3. YOU MUST USE THE SWITCH STATEMENT TO EXECUTE THE PROGRAM.

4. ALL MODIFICATIONS MUST BE DONE IN THE SAME ORIGINAL CHAR ARRAY WITHOUT CREATING A NEW ONE.

Write a C++ program that modifies a null teminated char array as follows:

Consonants are positioned at the beginning of the string and vowels are moved to the end of the string.

Example :

Original string : density

New string : dnstyei

Solutions

Expert Solution

CodeToCopy:

vowel_separation.cpp

#include <iostream>     /* for cout , cin objects */

#include <cstring>      /* for strcpy() function */

using namespace std;

int main() {

    /* taking input from user */

    string input;

    cout << "Original string: ";

    cin >> input;

   

    /* creating a character array using pointers to store the input */

    char * original_str = new char[input.size()];

   

    /* storing the input into character array */

    strcpy(original_str, input.c_str());

   

    int len = input.size();

   

    /* temporary array to store vowels positions in the array */

    bool temp[len];

   

    /* to hold the count of number of vowels */

    int num_vowels = 0;

    /* traversing character array to mark vowel positions and to count

     * number of vowels */

    for ( int i = 0; i < len; ++i) {

        switch(*(original_str + i)) {

            case 'a':

            case 'e':

            case 'i':

            case 'o':

            case 'u':

            case 'A':

            case 'E':

            case 'I':

            case 'O':

            case 'U':

            {

                /* mark vowel position */

                temp[i] = true;

               

                /* increment the vowel count */

                num_vowels++;

            }

            break;

           

            default:

                temp[i] = false;

            ;

        }

    }

   

    int index = 0;

   

    /* separates vowels by moving them to the end of the string */

    while (num_vowels) {

        if (temp[index] == true) {

           

            /* checking the second character in a swap is vowel or not */

            if (temp[len-num_vowels] != true) {

                /* as it is not vowel, un-mark the position,

                   so that in next traversal, it will be ignored */

                temp[index] = false;   

            }

            else {

                /* as it is a vowel, keep the mark of second character's position*/

                /* and unmark the position of first character in a swap */

                temp[len-num_vowels] = false;

            }

           

            /* swapping vowel character to the end character position */

            char curr_char = *(original_str + (len-num_vowels));

            *(original_str + (len-num_vowels)) = *(original_str + index);

             *(original_str + index) = curr_char;

           

            num_vowels--;

        }

       

        /* incrementing the index */

        index = (index + 1) % len;

    }

   

    /* printing the new string */

    cout << "New string: " << original_str << endl;

   

    /* freeing the allocated heap memory */

    delete original_str;

}

OutputScreenshot:



Related Solutions

Write a C++ function that accepts array size and pointers to three arrays a1, a2 and...
Write a C++ function that accepts array size and pointers to three arrays a1, a2 and a3 of type float as parameters. It then multiplies a1 and a2 and stored the result in a3. Assume that array multiplication is done by multiplying corresponding array elements, e.g. a3[i] = a1[i] * a2[i] where 0 <= i <= (array size – 1) Write a C++ main program to dynamically create three equal sized float arrays a1, a2 and a3. The size of...
I have this program in C that takes three char arrays that each have a first...
I have this program in C that takes three char arrays that each have a first and last name. I have two functions that reverese the name and change it to all upper case. I have the program completeed but need to change both functions to use pointers instead of arrays. I will bold the functions I need to use pointers. #include <stdio.h> void upper_string(char []); int main() { char name1[100]="John Smith"; char name2[100]="Mary Cohen"; char name3[100]="Carl Williams"; upper_string(name1);// calling...
Write a complete C program that searches an element in array using pointers. Please use the...
Write a complete C program that searches an element in array using pointers. Please use the function called search to find the given number. //Function Prototype void search (int * array, int num, int size)
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N...
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax (no [ ]’s or (ptr+i) stuff.     Input will be the number of input values to enter followed by the sum to compare with. Print out the continuous sub-array of values that are equal to sum or the message ‘No sum found’. There...
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105....
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105. In the boxes below, fill in what your array should have. Fill in the index of each element below it. Array Index Open up your editor and write the following program: Declare an array that has the numbers 100 to 105. (How many elements are there in the array?) Print the array. Save your file and test it. Compare your results with your table...
You need a data structure that contains fields for name (char array), color (char array), age...
You need a data structure that contains fields for name (char array), color (char array), age (int), and height (int). Name the struct Info when you define it. Create a function named getUserInfo() that asks the user for name, color, age, and height and then returns a data structure containing that information. It should do the following: What is your name? George What is your favorite color? Green What is your age? 111 What is your height in inches? 72...
6- Write a C++ program that determines the amount of memory used by char, short int,...
6- Write a C++ program that determines the amount of memory used by char, short int, unsigned short int, float, and double types and display the information on the screen. You program should also display the typical range for each data type.
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist...
Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist in the former array. They need not necessarily be in the same order. For example, [1,7,3,4,2] overlaps [1,2,3] because 1,2 and 3 exist in [1,7,3,4,2]. To make the implementation easy, [1,7,3,4,2] overlaps [1,1,2,3] as well. We don’t need to check whether 1 appears twice in the first array. Write a program that lets the user enter two arrays and displays whether the first array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT