I want and searching for a function or an alternative that could replace the non-standard getch() function of C language. I want help if any any function which can replace the getch() function from the code below or an appropriate alternative.
I am doing this since my college has asked me not to use the conio.h header file in my C programs.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
int i=0, j=0, memAlloc=1;
char *p, *q, a;
p=(char *)calloc(1, sizeof(char));
while(1)
{
a=getch(); //getch() function
if(a!='\b'&&a!='\r')
{
p[i]=a;
++i;
printf("%c", a);
memAlloc++;
p=realloc(p, memAlloc*sizeof(char));
}
if(a=='\b'&&i>=1)
{
printf("\b \b");
--i;
}
if(a=='\r')
{
p[i]='\0';
break;
}
}
printf("\n");
for (i=0; p[i]!='\0'; i++)
printf("%c", p[i]);
//Storing a string of only alphabets into a new dynamic array
q=(char *)calloc(1, sizeof(char));
memAlloc=1;
for (i=0; p[i]!='\0'; i++)
{
if (isalpha(p[I])) //Checking for alphabet
if(isupper(p[i]))
{
q[j]=p[i]+32; //If uppercase alphabet convert it to lowercase
memAlloc++;
q=realloc(q, memAlloc*sizeof(char));
j++;
}
else
{
q[j]=p[i];
memAlloc++;
q=realloc(q, memAlloc*sizeof(char));
j++;
}
}
q[j]='\0'; //Adding a null character to the end of the string
free(p);
printf("\n");
for (i=0; q[i]!='\0'; i++)
printf("%c", q[i]);
}
Here is the explanation of the program.
The program will take a string input from the user of unknown size and save it to the dynamic array. The user will keep on entering characters in a single line until he presses the enter key after which the whole string is saved to a dynamic array. The program then removes every character except alphabets and converting uppercase alphabets to it's lowercase and then saves the updated string to an another dynamic array.