0

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

2
  • What does "input is looping forever" and "input does not stop" mean exactly? Please give the exact run log. Commented Jan 3, 2022 at 10:10
  • Note: I'm limited to only able to use basic function for this program Commented Jan 3, 2022 at 11:46

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

I have tried to answer the question what's wrong with the code?, but not to give you directly the correct code. IMHO you will learn more if you find it by yourself...
0

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

6 Comments

Is there any replacement for enum, size_t, unsigned char, const, memset, sizeof, and putchar? Since I can't use that for the program either
@TheRealF6 size_t is the return type of the function strlen that you are using. So it sounds strange that you can not use it. Use the type int instead. memset can be substituted for a for loop.
I can't use the function because that's the requirement of the program. I can only use basic function.
I don't think I can use type int either. I can't use something that I haven't learn.
@TheRealF6 Your comments do not make a sense. You are already using the type int as in this declaration in your program int x, y;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.