Hello, I have this program that asks the user to enter 10 characters. I store these characters in an array and sort them using a bubble sort. However, when I run it, it reads in the Enter key and the character both, but the Enter key is not suppose to be a character. How can I fix this? I am trying to learn C but I am struggling. If anyone is willing to help me I will greatly appreciate it
Code:
#include <stdio.h>
#include <stdbool.h>
void bubbleSort(char *);
int main(){
char *arr=(char *)malloc(11*sizeof(char));
int i;
char input;
for( i=0;i<10;i++, input = '\0'){
printf("Enter character number %d:\n", i+1);
scanf("%c", (arr+i));
}
bubbleSort(arr);
printf("The sorted characters are:\n");
for(i=0;i<10;i++){
printf("%c ", *(arr+i));
}
printf("\n");
}
void bubbleSort(char *arr){
char temp;
bool swap;
do{
swap = false;
int i;
for( i = 0; i < 10; i++ )
{
if(*(arr + i)< *(arr + i + 1)){
temp = *(arr + i);
*(arr + i) = *(arr+ i +1 );
*(arr+ i + 1) = temp;
swap = true;
}
}
}while(swap);
}