Good day!
There is a program that receives characters at the input and translates them into hexadecimal system. How can I change the code so that the program handles only a certain number of characters?
For example: when n = 3, the program receiving the input "Hello", would process only Hel.
#include <stdio.h> #include <stdlib.h> #include <ctype.h> int main(void) { char in[255]; char out[255]; int len; char ch = '\0'; fgets(in, sizeof(in), stdin); len = strlen(in); if(in[len-1]=='\n') in[--len] = ch; for(int i = 0; i<len; i++) { sprintf(out+i*2, "%x", in[i]); } printf("%s\n", out); } PS: you cannot use string.h and malloc
strlen()? - PinkTuxstdlib.h. Secondly, you still usestrlen(). Third, it is possible to use string functions without connectingstring.h, it is C, not C ++. And fourth, for the future - a formal ban onmalloc()in many cases costs usingalloca():-) - PinkTux