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

Hello, I was wondering if anyone could help me with this C problem. Thank You! F

ID: 3723655 • Letter: H

Question

Hello, I was wondering if anyone could help me with this C problem. Thank You!

For this lab you will write a program that will read in one character per line until you hit a 0' (zero as a character), store those characters in an array, sort that array (from largest to smallest) and print out each character with a space between them (no newlines). Notes Modify the sort algorithm we went over in class to do this sort. The file that will have the characters in it is on brightspace and is called alphabet.txt. Remember how to do file redirection: myprog

Explanation / Answer

//program
#include <stdio.h>

//using bubble sort to sort the charArr
void bubbleSort(char arr[],int size);

int main()
{
FILE *fp;
char c;
int count = 0,i;
//declare an array for storing 26 characters
char charArr[26];
  
do
{
c = fgetc(stdin);
if(c == EOF)
break;
if(c >='a' && c<= 'z' || c>='A' && c<='Z')
{
charArr[count++]=c;
}
  
}while(c!=EOF);
//print the array before sort
printf("Array before sort: ");
for(i = 0; i < count; i++)
{
printf("%c ",charArr[i]);
}
printf(" ");
//printf("Count = %d ",count);
//call bubble sort
bubbleSort(charArr,count);
//print arr after sorting from highest to smallest alphabetical order
printf("Array After sort: ");
for(i = 0; i < count; i++)
{
printf("%c ",charArr[i]);
}
printf(" ");
return 0;
}

void bubbleSort(char arr[],int size)
{
int i,j;
char c;
  
for(i = 0; i < size-1;i++)
{
for(j = 0; j < size-1-i;j++)
{
if(arr[j] < arr[j+1])
{
//swap
c = arr[j];
arr[j] = arr[j+1];
arr[j+1]=c;
}
}
}
}


-----------------------------------------------------------------------------------------------
//alphabet.txt file
b
d
e
i
k
u
l
m
o
s
r
y
x
z
a
c
f
g
j
p
n
q
t
h
v
w

---------------------------

//run with command
./a.out < alphabet.txt


//output
Array before sort: b d e i k u l m o s r y x z a c f g j p n q t h v w   
Array After sort: z y x w v u t s r q p o n m l k j i h g f e d c b a

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote