This assignment covers some basic standard ANSI C programming using arrays of ch
ID: 3730315 • Letter: T
Question
This assignment covers some basic standard ANSI C programming using arrays of character arrays. You are to write a program to prompt the user for up to 10 sentences (each sentence has a maximum size of 100 characters). You must store your sentences in an array of character arrays. After the user indicates how many sentences they wish to enter, each must be allocated dynamically using the C malloc function. For example, your initial variable declaration might look like this: char **mySentences; Prompt the user and read in their sentences (see sample i/o below). Convert each sentence to upper case, and after gathering all of the sentences, print them out. Count the number of characters including white space and the number of characters not including white space. You MUST write functions to perform subtasks for this problem. These need to be in a separate .c file from the main program. At a minimum, you must implement the following two functions: void convertSentence(char *sentence); void convertAll(char **sentenceList, int numOfSentences); The first function takes a character array and converts it to all upper case. The second takes your array of all sentences and converts them all to uppercase. NOTE: The only built-in C utility for characters and character strings that you may use are toupper(), isspace(), get/fget, getc/fgetc, gets/fgets, printf, scanf, malloc, and free.
Explanation / Answer
main.c:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
void convertSentence(char* sentence); //declaring the functions as the functions are defined in other file
void convertAll(char **sentenceList,int numOfSentences);
int main()
{
int numberOfSentences,i;
int white=0,letter=0;
char **sentenceList;
char dummy; //so that the scanf works properly
printf("Please enter the number of sentences(Maximum 10) : ");
scanf("%d",&numberOfSentences); // number of sentences taken from the user
scanf("%c",&dummy); // it consumes the enter that we press after entering the number
sentenceList = malloc(sizeof(char*)* numberOfSentences); //allocating memory for sentences
for(i = 0;i<numberOfSentences;i++)
{
sentenceList[i] = malloc(sizeof(char)*100); // allocating memory for charecters
}
for(i = 0;i<numberOfSentences;i++)
{
printf("Please enter sentence %d ",i+1); //taking sentences as input.
gets(sentenceList[i]);
}
convertAll(sentenceList,numberOfSentences); // calling the function which will change the lower cases to upper cases.
for(i =0; i < numberOfSentences;i++)
{
int j =0;
while(sentenceList[i][j])
{
if(isspace(sentenceList[i][j]))
{
white++; //counting the spaces
}
else
letter++; //counting the letters
j++;
}
}
for(i=0;i<numberOfSentences;i++)
printf("%s ",sentenceList[i]); // printing the uppercase converted sentences
printf("The number of spaces is %d ",white); // priting the number of white spaces
printf("The number of letters is %d ",letter); // printing the number of letters
for(i=0;i<numberOfSentences;i++)
{
free(sentenceList[i]); // freeing up the memory allocated using malloc
}
free(sentenceList); // freeing up the memory allocated using malloc
return 0;
}
functions.c: //this file contains the two functions used in main.
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
void convertSentence(char *sentence) //this functions converts a sentence to uppercase
{
int i =0;
while(sentence[i])
{
if(!isspace(sentence[i])) //before calling we are checking if it is a letter or not.
{
sentence[i] = toupper(sentence[i]); // converting the letter to upper case.
}
i++;
}
}
void convertAll(char **sentenceList, int numOfSentences) // converting all the sentences
{
int i = 0;
for(i =0; i<numOfSentences;i++)
{
convertSentence(sentenceList[i]); // using the function defined above on every sentences.
}
}
Compilation:
$ gcc main.c function.c
Run:
$ ./a.out
Please enter the number of sentences(Maximum 10) :
2
Please enter sentence 1
super heterodyne reciever
Please enter sentence 2
neuclear atomic fusion
SUPER HETERODYNE RECIEVER
NEUCLEAR ATOMIC FUSION
The number of spaces is 4
The number of letters spaces is 43
p.s: If you like this answer please give a thumbs up, and if you dont like or feel that the answer needs more information feel free to comment and give a feed back.
Thank you!
Cheers ! Happy Coding.