The task: the source string ( char ) contains the numbers of the decimal number system. Find them, create a new string in which to replace the numbers of the decimal number system with their own denomination in hexadecimal number system. Nechisla must be left in its place.
Example: 444444asdf should be converted to 6C81Casdf .
You cannot use standard functions and libraries.
My code can only translate numbers, if the string contains letters, it does not work. Please assist.
My implementation:
#include <stdio.h> void inputChar(char *str) { printf("input array char <100: "); scanf("%s", str); } void revers(char *A) { int j; for (j = 0; A[j] != '\0'; j++); j--; for (int i = 0; i <j; i++, j--) { char temp = A[i]; A[i] = A[j]; A[j] = temp; } } int charToDecInt(char *A) { int summ = 0; for (int i = 0; A[i] != '\0'; i++) { if ('0' <= A[i] && A[i] <= '9') { summ *= 10; summ += A[i] - '0'; } else { summ += A[i]-'A'+10; } } return summ; } void decIntTohexString(int a, char *A) { int j = 0; while (a != 0) { int r = a % 16; if (r >= 1 && r <= 9) { r += '0'; } else { r += 'A'- 10; } A[j++] = r+A[j]; a /= 16; } A[j] = '\0'; revers(A); } void display() { const int N = 100; char A[N] = ""; char B[N] = ""; inputChar(A); int a = charToDecInt(A); decIntTohexString(a, B); printf("hexChar = %s\n", B); } int main() { display(); return 0; }