164k views
5 votes
Write a main function to do the following: Create a string that can hold up to 30 characters. Create an array of 10 strings with room for 60 characters plus the null byte. Prompt the user and read the name of an input file into your string. Open the file for input. Call your function to read from the file into the array. Read into all 10 strings. Call your function to remove the newlines at the end of all of the strings. Call your function to print all of the strings. Print the smallest string. Change all of the strings to uppercase. Print all of the strings again.

1 Answer

6 votes

Answer:

Check the explanation

Step-by-step explanation:

#// do comment if any problem arises

//code

#include <stdio.h>

#include <ctype.h>

#include <string.h>

#include <stdlib.h>

void read(FILE *input, char array[10][61])

{

// read all 10 strings

for (int i = 0; i < 10; i++)

fgets(array[i], 60, input);

}

void remove_newline(char array[10][61])

{

for (int i = 0; i < 10; i++)

{

array[i][strlen(array[i]) - 1] = '\0';

}

}

void smallest(char array[10][61])

{

int s = 0;

for (int i = 1; i < 10; i++)

{

if (strlen(array[s]) > strlen(array[i]))

s = i;

}

printf("\\Smallest strnig: %s\\", array[s]);

}

void upper(char array[10][61])

{

for (int i = 0; i < 10; i++)

{

int n = strlen(array[i]);

for (int j = 0; j < n; j++)

{

array[i][j] = toupper(array[i][j]);

}

}

}

int main()

{

char filename[30];

// array of 10 strings

char array[10][61];

printf("Enter filename: ");

gets(filename);

// open file

FILE *input = fopen(filename, "r");

// read into array

read(input, array);

// close file

fclose(input);

// remove newline

remove_newline(array);

// print

printf("Contents of file:\\");

for (int i = 0; i < 10; i++)

printf("%s\\", array[i]);

// print smallest string

smallest(array);

// uppercase

upper(array);

// print again

printf("\\Contents of file after converting to uppercase:\\");

for (int i = 0; i < 10; i++)

printf("%s\\", array[i]);

}

kindly check the attached image below to see the Output:

Write a main function to do the following: Create a string that can hold up to 30 characters-example-1
User Cereal Killer
by
5.8k points