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: 3790620 • 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) {    }  // 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]) {      }  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). // See the output provided in the word document for example input and output. void sortStrings(char strings[NUM_STRINGS][STRING_LENGTH]) {   }  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

PROGRAM CODE:

/*
* stringManipulation.cpp
*
* Created on: 11-Feb-2017
* Author: kasturi
*/


#include <stdio.h>
#include <string.h>
#include <ctype.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)
{
   char *tobeModified = &strings[0][0];
   int i=0;
   while(i<NUM_STRINGS)
   {
       int counter = 0, j=0;

       while(j<STRING_LENGTH)
       {

           if(*tobeModified == search_alphabet)
           {
               counter++;
           }
           tobeModified++;
           j++;
           if(*tobeModified == NULL)
           {
               char c = counter + '0';
               *tobeModified = c;
               tobeModified++;
               *tobeModified = '';
               j++;
               tobeModified += (STRING_LENGTH-j);
               break;
           }
       }

       i++;
   }
}

// 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])
{
   char *tobeModified = &strings[0][0];
   int counter = STRING_LENGTH;
   char nextValue;
   int i=0;
   while(i<NUM_STRINGS)
   {

       if(*tobeModified == 'a' || *tobeModified == 'e' || *tobeModified == 'i' || *tobeModified == 'o' || *tobeModified == 'u')
       {
           nextValue = *(tobeModified +1);
           *tobeModified = nextValue;
           char *temp = tobeModified;
           while(*temp != NULL)
           {
               temp++;
               *temp = *(temp + 1);
           }
       }
       else
           {   tobeModified++;
               counter--;
           }
       if(*tobeModified == NULL)
       {
           tobeModified +=counter;
           i++;
           counter = STRING_LENGTH;
       }
   }
}

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).
// See the output provided in the word document for example input and output.
void sortStrings(char strings[NUM_STRINGS][STRING_LENGTH])
{
   char *temp1 = &strings[0][0];
   for(int i=0; i<NUM_STRINGS; i++)
   {
       char *temp2 = temp1;
       for(int j=i+1; j<NUM_STRINGS; j++)
       {
           temp2 += STRING_LENGTH;
           if(strcmp(temp1, temp2) > 0)
               swapStrings(temp1, temp2);
       }
       temp1 += STRING_LENGTH;
   }
}

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])
{
   int counter = 0, i=0;
   char *pointer = &string[0];
   while(i<STRING_LENGTH)
   {
       if(isalpha(*pointer))
           counter++;
       pointer++;
       i++;
   }
   return counter;
}

// 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])
{
   char *beginning = &string[0];
   char *ending = &string[strlen(string)-1];
   int length = (strlen(string) - 2)/2;
   int i=0;
   while(i<length)
   {
       if(*beginning != *ending)
           return 0;
       else {
           beginning++;
           ending--;
           i++;
       }
   }
   return 1;
}

// 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)
{

   int i = 0;
   char *tobeModified = &string[0];
   tobeModified += pos;
   i = pos;
   char prevValue = *tobeModified;
   char currentValue;
   *tobeModified = letterToAdd;
   tobeModified++;
   while(i<STRING_LENGTH)
   {
       currentValue = *tobeModified;
       *tobeModified = prevValue;
       tobeModified++;
       i++;
       prevValue = currentValue;
   }

}


// You should study and understand how this main function works.
// Do not modify it in any way, there is no implementation needed here.
int 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);
printStrings(strings);
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...");
}
return 0;
}

OUTPUT:

Run #1:

Assignment 4: Pointer Operations

Choose one of the following:

1. Sorting Strings

2. Alphabet counter

3. Palindrome

4. Letter Addition

1

Enter the next String: elephant

Enter the next String: mango

Enter the next String: tree

Enter the next String: screw

Enter the next String: strip

Enter a character for checking its frequency: e

elephant2

mango0

tree2

screw1

strip0

The strings after vowel removal:

lphnt2

mng0

tr2

scrw1

strp0

Sorted Strings:

lphnt2

mng0

scrw1

strp0

tr2

Run #2:

Assignment 4: Pointer Operations

Choose one of the following:

1. Sorting Strings

2. Alphabet counter

3. Palindrome

4. Letter Addition

2

Enter a String: chelsea

There are 7 alphabets in "chelsea"

Run #3:

Assignment 4: Pointer Operations

Choose one of the following:

1. Sorting Strings

2. Alphabet counter

3. Palindrome

4. Letter Addition

3

Enter a String: racecar

The string "racecar" is a palindrome

Run #4:

Assignment 4: Pointer Operations

Choose one of the following:

1. Sorting Strings

2. Alphabet counter

3. Palindrome

4. Letter Addition

4

Enter a String: helo

Enter a letter to be added: l

Enter the array position for adding the letter:2

Result: hello