简体   繁体   中英

Converting a char string into ASCII numbers and then separate the ASCII numbers & convert it into a 2d array using C language

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..)

What do think this part of the code will do:

for (x = 0; x < strlen(str); x++) {
    for (y = 0; y = strlen(str); y++) {
        asc[x][y] = ascii[x]; //separate ascii to 2d array
    }
}

First of all, you should notice that if you input funny , strlen(str) will be five when the inner array is declared with a size of 3. So it is a hint that an error is likely to be there...

Next, you want to do a base 10 decomposition, so I would expect divisions and remainder by 10 which are never present in your code.

Remember: you best friends here will be a paper sheet and a pencil. Write down in pseudo code or simply with math operators by hand what you want to do. I expect:

  • modulo 10 operations (at least 2 times per character)
  • division by 10 operation (at least 2 times per character)

Once this part will be done (and only then) it will be time to go back to your keyboard...

Firstly the argument type of this call of scanf

scanf("%s", &str);

is incorrect. You need to write at least like

scanf("%s", str);

This array declaration and the corresponding for loop

int i;
int ascii[50];
for(i = 0; i < strlen(str); i++){
    ascii[i] = str[i]; //converting string to ascii
}

are redundant. You could work directly with the array

int asc[50][3];

Moreover this statement

ascii[i] = str[i];

should be rewritten like

ascii[i] = ( unsigned char )str[i];

These nested for loops

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("-");
    }
}

are invalid because the second index of the array asc must be in the range [0, 3 ) .

The program can look the following way

#include <stdio.h>
#include <string.h>

int main( void )
{
    enum { N = 50 };
    char s[N];
    s[0] = '\0';

    printf( "Input: " );
    scanf( "%49s", s );

    size_t n = strlen( s );

    unsigned char a[n][3];
    
    for ( size_t i = 0; i < n; i++ )
    {
        const unsigned char Base = 10;
        
        memset( a[i], 0, sizeof( a[i] ) );
        unsigned char c = s[i];
        
        for ( size_t j = sizeof( a[i] ); c; c /= Base )
        {
            a[i][--j] = c % Base + '0';
        }
    }
    
    for ( size_t i = 0; i < n; i++ )
    {
        if ( i != 0 ) 
        {
            putchar( '-' );
            putchar( '-' );
        }
        
        for ( size_t j = 0; j < sizeof( a[i] ); j++ )
        {
            if ( j != 0 ) putchar( '-' );
            putchar( a[i][j]  );
        }
    }
    
    putchar( '\n' );
}

The program output might be

Input: fun
1-0-2--1-1-7--1-1-0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM