I wanted to make a program that converts my string input (char) into ASCII numbers and then separates the ASCII number and convert it into a 2-dimensional array and then display the 2d array in the output like this.
Input: fun (str is the name of the string that I use for input)
//The string char input
str = fun
The program then converts the input (string char) into ASCII numbers. (ascii is the name of the array/string that I use for the ASCII numbers)
//ASCII numbers for 'fun' (f u n)(left to right)
ascii = 102 117 110
Note: 102 is a letter 'f' in ASCII, 117 is a letter 'u' in ASCII, and 110 is a letter 'n' in ASCII.
Then, the program separates ASCII numbers and converts it into a 2d array. ('asc' is the name of the 2d array)
//ASCII for letter 'f'
asc[0][0] = 1
asc[0][1] = 0
asc[0][2] = 2
//ASCII for letter 'u'
asc[1][0] = 1
asc[1][1] = 1
asc[1][2] = 7
//ASCII for letter 'n'
asc[2][0] = 1
asc[2][1] = 1
asc[2][2] = 0
Output: 1-0-2--1-1-7--1-1-0
Here's the input and expected output of this program:
Input: fun
Output: 1-0-2--1-1-7--1-1-0
Here's the full code that I made:
#include <stdio.h>
#include <string.h>
int main(){
char str[50];
printf("Input: ");
scanf("%s", &str); //string input
int i;
int ascii[50];
for(i = 0; i < strlen(str); i++){
ascii[i] = str[i]; //converting string to ascii
}
int x, y;
int asc[50][3];
for(x = 0; x < strlen(str); x++){
for(y = 0; y = strlen(str); y++){
asc[x][y] = ascii[x]; //separate ascii to 2d array
}
}
printf("Output: ");
for(x = 0; x < strlen(str); x++){
for(y = 0; y < strlen(str); y++){
printf("%d", asc[x][y]); //showing the result of separation/conversion
printf("-");
}
}
return 0;
}
Can you tell me what's wrong with the code? It doesn't output the wanted/expected result. Instead, the input is looping forever (the input does not stop). Thank you in advance for giving me a solution to this problem!
(Note: The program is limited to only able to use two header (stdio.h and string.h). The program also limited to only able to use three str function (strcpy, strcmp, strlen)(How much of it in the program is infinite though.). I also can't use gets, puts, define, etc..)