+ 1

This Program is working on gcc, clang and tcc compilers with modes: -Wall -pedantic but here it is not working properly

#include <stdio.h> #include <stdlib.h> #include <ctype.h> int main() { char* str= (char*)malloc(sizeof(char)); str[0]= '\0'; int i=0; char get; get=getchar(); while( get!='\0' && get!='\n') { i++; if(isupper(get)){ str= (char*)realloc(str, (i+1)* sizeof(char)); str[i-1]= '_'; i++; } str= (char*)realloc(str, (i+1)* sizeof(char)); str[i-1]= tolower(get); str[i]='\0'; get=getchar(); } puts(str); return 0; }

1st Nov 2024, 8:44 PM
Chandan Das
Chandan Das - avatar
10 ответов
+ 3
Your program loops until it receives a NULL or a NEWLINE. In SoloLearn, that's breaking. To make this program work, when you input the data to run the script, add a NEWLINE at the end of your data. Type your word, press ENTER, your cursor goes to the next line. THEN submit it. It will work. To fix this, you'll need to test the condition differently. I don't know why - but in SoloLearn, after the end of the characters it starts returning -1. So you can fix your program by testing for -1. while( get!='\0' && get!='\n' && get!= -1) NOTE: How I solved this was I added a print statement below the second get=getchar(); line. printf("%c:%d\n", get, get); That prints the character being input as well as the integer value of that character.
1st Nov 2024, 10:32 PM
Jerry Hobby