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> #include <stdlib.h> // Read before you st

ID: 3799307 • Letter: #

Question

#include <stdio.h>
#include <string.h>
#include <stdlib.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 ASU General and GNU GCC environment.
// 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 an integer, an integer will be input.
// NOTE: For this assignment, you can assume that all input characters will be lowercase characters.

// Global Macro Values. They are used to define the size of a 5 x 32 two-dimensional array of characters

#define NUM_STRINGS 5
#define STRING_LENGTH 32

// Forward Declarations

void frequency(char **strings, char search_alphabet);
void remove_vowel(char **strings);
void swapStrings(char *string1, char *string2);
void sortStrings(char **strings);
void printStrings(char **strings);


/* Problem 1: Input data (5 points)
Create a text file called inputdata.txt.
Place the file into the same folder as your program.
Enter the following five strings into the text file:
apple
grapes
pineapple
coconut
orange
*/


// Problem 2: frequency (5 points)
// Traverse the 2D array of characters variable 'strings' and check the frequency of a particular letter or a search_alphabet in 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.
// Note: You must use pointer operations.
void frequency(char **strings, char search_alphabet)
{

}

// Problem 3: remove_vowel (5 points)
// 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.
// Note: You must use pointer operations.
void remove_vowel(char **strings)
{

}

// Problem 4: swapStrings (5 points)
// Swap the strings that are passed as parameters, this function will be used in Problem 5.
// If string1 is "hello" and string2 is "goodbye", after calling this function, string1 will be "goodbye" and string2 will be "hello".
// Note: You must use pointer operations.
void swapStrings(char *string1, char *string2)
{

}

// Problem 5: sortStrings (20 points)
// 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.
// Note: You must use pointer operations.
void sortStrings(char **strings)
{

}

// Problem 6: printStrings (10 points)
// Traverse the 2D character array "strings" and print each of the contained strings. Put each string in a separate line.
// Note: You must use pointer operations.
void printStrings(char **strings)
{

}

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

   char **strings; // will store 5 strings each with a max length of 32
   char search_alphabet;
   char input[STRING_LENGTH];
   int i;
  
   strings = (char **)malloc(sizeof(char *) * NUM_STRINGS);
   for (i = 0; i < NUM_STRINGS; i++)
       *(strings + i) = (char *)calloc(STRING_LENGTH, sizeof(char));
  
   printf("Project 3: 2D Character Arrays ");

   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);
  
   for (i = 0; i < NUM_STRINGS; i++)
   {
       free(*(strings + i));   // gabage collection
   }
   free(strings);
}

Explanation / Answer

#include <stdio.h>
#include <string.h>
#include <stdlib.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 ASU General and GNU GCC environment.
// 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 an integer, an integer will be input.
// NOTE: For this assignment, you can assume that all input characters will be lowercase characters.
// Global Macro Values. They are used to define the size of a 5 x 32 two-dimensional array of characters
#define NUM_STRINGS 5
#define STRING_LENGTH 32
// Forward Declarations
void frequency(char **strings, char search_alphabet);
void remove_vowel(char **strings);
void swapStrings(char *string1, char *string2);
void sortStrings(char **strings);
void printStrings(char **strings);

/* Problem 1: Input data (5 points)
Create a text file called inputdata.txt.
Place the file into the same folder as your program.
Enter the following five strings into the text file:
apple
grapes
pineapple
coconut
orange
*/

// Problem 2: frequency (5 points)
// Traverse the 2D array of characters variable 'strings' and check the frequency of a particular letter or a search_alphabet in 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.
// Note: You must use pointer operations.

int check_vowel(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;
default:
return 0;
}
}
void frequency(char **strings, char search_alphabet)
{

int i,j;
for ( i = 0; i < NUM_STRINGS; ++i)
{
int count=0;
printf("Count of character %c in %s ",search_alphabet,strings[i]);

for ( j = 0; j < strlen(strings[i]); ++j)
{
if(strings[i][j]==search_alphabet)
count++;
}
printf("%d ",count);
}

}

// Problem 3: remove_vowel (5 points)
// 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.
// Note: You must use pointer operations.
void remove_vowel(char **strings)
{int i,j,k;

for (k = 0; k < NUM_STRINGS; ++k)
{
int len=strlen(strings[k]);
for(i=0; i<strlen(strings[k]); i++)
{
if(strings[k][i]=='a' || strings[k][i]=='e' || strings[k][i]=='i' ||
strings[k][i]=='o' || strings[k][i]=='u' || strings[k][i]=='A' ||
strings[k][i]=='E' || strings[k][i]=='I' || strings[k][i]=='O' ||
strings[k][i]=='U')
{
for(j=i; j<len; j++)
{
strings[k][j]=strings[k][j+1];
}
len--;
}
}
}


}

// Problem 4: swapStrings (5 points)
// Swap the strings that are passed as parameters, this function will be used in Problem 5.
// If string1 is "hello" and string2 is "goodbye", after calling this function, string1 will be "goodbye" and string2 will be "hello".
// Note: You must use pointer operations.
void swapStrings(char *string1, char *string2)
{char* temp;
strcpy(temp,string1);
strcpy(string1,string2);
strcpy(string2,temp);
}

// Problem 5: sortStrings (20 points)
// 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.
// Note: You must use pointer operations.
void sortStrings(char **strings)
{int i,j;
char temp[20];
for(i=0;i<NUM_STRINGS;i++)
for(j=i+1;j<NUM_STRINGS;j++){
if(strcmp(strings[i],strings[j])>0){
strcpy(temp,strings[i]);
strcpy(strings[i],strings[j]);
strcpy(strings[j],temp);
}
}

}

// Problem 6: printStrings (10 points)
// Traverse the 2D character array "strings" and print each of the contained strings. Put each string in a separate line.
// Note: You must use pointer operations.
void printStrings(char **strings)
{int i;
for ( i = 0; i < NUM_STRINGS; ++i)
{
printf("%s ",strings[i] );
}
}

// 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()
{
char **strings; // will store 5 strings each with a max length of 32
char search_alphabet;
char input[STRING_LENGTH];
int i;
  
strings = (char **)malloc(sizeof(char *) * NUM_STRINGS);
for (i = 0; i < NUM_STRINGS; i++)
*(strings + i) = (char *)calloc(STRING_LENGTH, sizeof(char));
  
printf("Project 3: 2D Character Arrays ");
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);
  
for (i = 0; i < NUM_STRINGS; i++)
{
free(*(strings + i)); // gabage collection
}
free(strings);
}

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

Output:

akshay@akshay-Inspiron-3537:~/Chegg$ gcc sss.c
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Project 3: 2D Character Arrays

Enter the next String: apple
Enter the next String: pineapple
Enter the next String: coconut
Enter the next String: oranges
Enter the next String: grapes
Enter a character for checking its frequency: e
Count of character e in apple
1
Count of character e in pineapple
2
Count of character e in coconut
0
Count of character e in oranges
1
Count of character e in grapes
1

The strings after vowel removal:
ppl
pnappl
ccnt
rngs
grps

Sorted Strings:
ccnt
grps
pnappl
ppl
rngs