Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <stdio.h> #include <string.h> // Read before you start: // Do not modif

ID: 3791719 • Letter: #

Question

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


// Read before you start:
// Do not modify any part of this program that you are given. Doing so may cause you to fail automated test cases.
// You are given a partially complete program. Your job is to complete the functions in order for this program to work successfully.
// You should complete this homework assignment using Microsoft Visual Studios 2013 (or a later version).
// All instructions are given above the required functions, please read them and follow them carefully.
// If you modify the function return types or parameters, you will fail the automated test cases.
// You can assume that all inputs are valid. Ex: If prompted for a char, the input will be a char.

// Global Macro Values
#define NUM_STRINGS 5
#define STRING_LENGTH 32

// Forward Declarations

void frequency(char[NUM_STRINGS][STRING_LENGTH], char);
void remove_Number(char[NUM_STRINGS][STRING_LENGTH]);
void swapStrings(char[STRING_LENGTH], char[STRING_LENGTH]);
void sortStrings(char[NUM_STRINGS][STRING_LENGTH]);
void printStrings(char[NUM_STRINGS][STRING_LENGTH]);
int alpha_Counter(char[STRING_LENGTH]);
int isAPalindrome(char[STRING_LENGTH]);
void addLetter(char[STRING_LENGTH], char, int);

// Problem 1: frequency (5 points)
// Rewrite this function to perform the same task as in hw03, using only pointer operations.
// You must use pointer operations only. If you use array operations, you will recieve no credit for this part.
// You may use the code you submitted for hw03 or you may use the solution code for hw03.
// Traverse the 2D array of characters variable 'strings' and check the frequency of a particular letter or a search_alphabetin a string.
// In order to check the frequency, first you need to read the search_alphabet from the user.
// If the string is "hello" and the search_alphabet is l, the code will count the number of 'l's in hello.
// The output of the function for the above mentioned case will be 2.
//append that frequency value at the end of the string
//for hello the new string will be hello2

void frequency(char strings[NUM_STRINGS][STRING_LENGTH], char search_alphabet)
{
   int i, j, count;

   for (i = 0; i < NUM_STRINGS; i++) {
       count = 0;
       j = 0;
       while (strings[i][j] != '')

       {

           if (strings[i][j] == search_alphabet)

               count++;
           j++;

       }
       strings[i][j] = '0' + count;
       strings[i][j + 1] = '';
   }

}

// Problem 2: remove_vowel (5 points)
// Rewrite this function to perform the same task as in hw03, using only pointer operations.
// You must use pointer operations only. If you use array operations, you will recieve no credit for this part.
// You may use the code you submitted for hw03 or you may use the solution code for hw03.
//Traverse the 2D array of characters variable 'strings' and remove all vowels from the string.
// In order to remove all vowel characters, you need to check each letter of the string and decide whether its is a vowel. If so then remove it. If not then check the next character.
// If the string is "hello", your result will be hll.
//print the new string without vowel using problem 6.

void remove_vowel(char strings[NUM_STRINGS][STRING_LENGTH])
{
   int i, j, k, len;

   for (i = 0; i < NUM_STRINGS; i++) {

       len = strlen(strings[i]);

       for (j = 0; j<len; j++)

       {

           if (strings[i][j] == 'a' || strings[i][j] == 'e' || strings[i][j] == 'i' || strings[i][j] == 'o' || strings[i][j] == 'u')
           {
               for (k = j; k<STRING_LENGTH; k++)
               {
                   strings[i][k] = strings[i][k + 1];
               }
               strings[i][k - 1] = '';
               j--;
           }


       }

   }
}

void swapStrings(char string1[STRING_LENGTH], char string2[STRING_LENGTH])
{
   char temp[STRING_LENGTH];
   strcpy(temp, string1);
   strcpy(string1, string2);
   strcpy(string2, temp);
}

// Problem 3: sortStrings (10 points)
// Rewrite this function to perform the same task as in hw03, using only pointer operations.
// You must use pointer operations only. If you use array operations, you will recieve no credit for this part.
// You can use the swapStrings() function if you'd like, but are not required to do so.
// You may use the code you submitted for hw03 or you may use the solution code for hw03.
//
// Sort the 5 strings contained in the 2D character array parameter labeled "strings".
// Sort the strings based on their ASCII character value (use strcmp to compare strings).
// NOTE: You MUST incorporate your "swapStrings" function to recieve full points for this part.
// See the output provided in the word document for example input and output.
void sortStrings(char strings[NUM_STRINGS][STRING_LENGTH])
{
   int i, j, min;

   for (i = 0; i < NUM_STRINGS - 1; i++)

   {

       int min = i;

       for (j = i + 1; j < NUM_STRINGS; j++)

       {

           if (strcmp(strings[min], strings[j]) > 0)

               min = j;

       }

       swapStrings(strings[i], strings[min]);

   }

}

void printStrings(char strings[NUM_STRINGS][STRING_LENGTH])
{
   int i;
   for (i = 0; i < NUM_STRINGS; i++)
   {
       printf("%s ", strings[i]);
   }
}

// Problem 4: vowelCounter (10 points)
// This function accepts an array of characters and returns the number of alphabets in that string (an integer).
// You must use pointer operations only. If you use array operations, you will recieve no credit for this part.
// you should not count any number or special character within the string
int alpha_Counter(char string[STRING_LENGTH])
{


}

// Problem 5: isAPalindrome (10 points)
// This function accepts an array of characters and returns an integer.
// You must use pointer operations only. If you use array operations, you will recieve no credit for this part.
// This function should return 1 (true) if the parameter 'string' is a palindrome, or 0 (false) if 'string' is not a palindrome.
// A palindrome is a sequence of characters which when reversed, is the same sequence of characters.
// For this assignment, you can assume that 'string' will be a single word containing only lowercase letters and no spaces.
// Example Palindromes: mom, racecar, stats, rotator, deleveled
int isAPalindrome(char string[STRING_LENGTH])
{


}

// Problem 6: addLetter (10 points)
// This function accepts an array of characters as well as a character to be added to the existig string and a position where this new letter is to be added.
// You must use pointer operations only. If you use array operations, you will recieve no credit for this part.
// All occurances of the 'letterToBeRemoved' should be removed from character array 'string'
// Example: If string = "letter", and letterToAdd = 'a'; the pos=2 after this function terminates, string should contain "leatter"
void addLetter(char string[STRING_LENGTH], char letterToAdd, int pos)
{

}


// You should study and understand how this main function works.
// Do not modify it in any way, there is no implementation needed here.
void main()
{
   int selection, i;
   char input[STRING_LENGTH];

   printf("Assignment 4: Pointer Operations ");
   printf("Choose one of the following: 1. Sorting Strings 2. Alphabet counter 3. Palindrome 4. Letter Addition ");
   scanf("%d", &selection); // store integer
   getchar(); // consume newline char

   if (selection == 1)
   {
       char strings[NUM_STRINGS][STRING_LENGTH]; // will store 5 strings each with a max length of 32
       char search_alphabet;

       for (i = 0; i < NUM_STRINGS; i++)
       {
           printf(" Enter the next String: "); // prompt for string
           fgets(input, sizeof(input), stdin); // store input string
           input[strlen(input) - 1] = ''; // convert trailing ' ' char to '' (null terminator)
           strcpy(strings[i], input); // copy input to 2D strings array
       }


       printf("Enter a character for checking its frequency: "); // prompt for integer
       scanf("%c", &search_alphabet); // store integer
       frequency(strings, search_alphabet);
       remove_vowel(strings);
       printf(" The strings after vowel removal: ");
       printStrings(strings);
       sortStrings(strings);
       printf(" Sorted Strings: ");
       printStrings(strings);

   }
   else if (selection == 2)
   {
       printf(" Enter a String: "); // prompt for string
       fgets(input, sizeof(input), stdin); // store input string
       input[strlen(input) - 1] = ''; // convert trailing ' ' char to '' (null terminator)

       int numAlpha = alpha_Counter(input);

       printf(" There are %d alphabets in "%s"", numAlpha, input);
   }
   else if (selection == 3)
   {
       printf(" Enter a String: "); // prompt for string
       fgets(input, sizeof(input), stdin); // store input string
       input[strlen(input) - 1] = ''; // convert trailing ' ' char to '' (null terminator)

       int isPalindrome = isAPalindrome(input);

       if (isPalindrome)
           printf(" The string "%s" is a palindrome", input);
       else
           printf(" The string "%s" is not a palindrome", input);
   }
   else if (selection == 4)
   {
       printf(" Enter a String: "); // prompt for string
       fgets(input, sizeof(input), stdin); // store input string
       input[strlen(input) - 1] = ''; // convert trailing ' ' char to '' (null terminator)

       char letterToAdd;
       int pos;
       printf(" Enter a letter to be added: "); // prompt for char
       scanf(" %c", &letterToAdd); // store input char
       printf(" Enter the array position for adding the letter:");
       scanf("%d", &pos);
       addLetter(input, letterToAdd, pos);

       printf(" Result: %s", input);
   }
   else
   {
       printf("Program terminating...");
   }
}

Explanation / Answer

Implement part 4 and part 5

// Problem 4: vowelCounter (10 points)
// This function accepts an array of characters and returns the number of alphabets in that string (an integer).
// You must use pointer operations only. If you use array operations, you will recieve no credit for this part.
// you should not count any number or special character within the string
int alpha_Counter(char string[STRING_LENGTH])
{
char *str = string;
int count = 0;
while (*str != '')
{

if (isalpha(*str)) /* counting number of vowels */
    {  
    switch (*str)
   {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
               ++count;
               break;
         }
}
str++;
}

return count;

}




// Problem 5: isAPalindrome (10 points)
// This function accepts an array of characters and returns an integer.
// You must use pointer operations only. If you use array operations, you will recieve no credit for this part.
// This function should return 1 (true) if the parameter 'string' is a palindrome, or 0 (false) if 'string' is not a palindrome.
// A palindrome is a sequence of characters which when reversed, is the same sequence of characters.
// For this assignment, you can assume that 'string' will be a single word containing only lowercase letters and no spaces.
// Example Palindromes: mom, racecar, stats, rotator, deleveled
int isAPalindrome(char string[STRING_LENGTH])
{

   int len = strlen(string);

    // handle empty string and string of length 1:
    if (len == 0) return 0;
    if (len == 1) return 1;

    char *ptr1 = string;
    char *ptr2 = string + len - 1;
    while(ptr2 >= ptr1)
{
        if (!isalpha(*ptr2))
  {
            ptr2--;
            continue;
        }
        if (!isalpha(*ptr1))
  {
            ptr1++;
            continue;
        }
        if( tolower(*ptr1) != tolower(*ptr2))
  {
            return 0;
        }
        ptr1++; ptr2--;
    }
   
return 1;

}