I don't know how to replace input char with appropriate integer

Hi guys, I asked for help on programming forums and no one didn't helped me so I ask for help here. I am playing with some tasks from my book and I can't figure where did I get wrong.

From the first program I get a blank screen, program won't generate 10*10 matrix.

And second problem is I can't figure out how to replace chars input
by the user with corresponding number.

Random walk program

Matrix program

Other program

Letters2number

Thanks for reading

If that's all you asked, I'm not surprised you got no answer.

BE SPECIFIC. We can't see your book from here. We don't know what "chars input" is supposed to be, and how your program, whatever it's doing, is not doing it.

Also, if this is homework, you need to post in the homework forum using the homework rules.

I apologise.

Ok for first program as you can see from source code I have aplhabet and
10x10 array. Then using rand () function program choose one of he following moves (up, down, right, left). If postition is free then in that place comes a letter from array aplhaber. Problem is that program does't generate final 10x10 matrix.

For second program problem is simple but I really don't know how to resolve it.
Program must convert input character to approriate number, if you have a close look then you shell see program traslates american style of phone to numeric.

For example if I enter 1-800-COL-LECT
then output must be 1-800-265-5328
my output is:

I see the problem with your matrix program. You're using counter to decide when to end, but you're also using it as an array index and preventing it from walking off the edge. So it'll never get higher than 8, and never end.

You're also preventing it from overwriting itself, i.e. changing any characters other than '.', so it'd be possible for itself to wall itself in like the worm game and freeze.

---------- Post updated at 10:32 AM ---------- Previous update was at 10:21 AM ----------

In your second program, you're reading numbers when you should be reading a string. You don't even check how many numbers you read, so you just go right into data you never read and print all kinds of garbage.

I'd do it like this:

char buf[128];
int n;

fprintf(stderr, "Input string: "); // no newline, so need stderr or fflush(stdout)
fgets(buf, 128, stdin); // read one exact line.

for(n=0; buf[n] != '\0'; n++)
{
        int c;

        if((buf[n] >= 'A') && (buf[n] <= 'Z')) c=buf[n]-'A'; //A=0, B=1,...
        else if((buf[n] >= 'a') && (buf[n] <= 'z')) c=buf[n]-'a';//a=0,b=1,...
        else // print everything else raw, and skip conversion
        {
                printf("%c", buf[n]);
                continue;
        }

        printf("%d", (c/3) + 1);
}

Thanks for your reply.

I didn't touch chapter with strings in my textbook, I only have knowledege about basic things in C, but as my teach said: go slowly and win :slight_smile:

Now about your program, I compiled it and works but don't works correctly.

Input string: 1-800-col-lect
1-800-154-4217

But anyway thanks for help

$
$
$
$ echo "1-800-COL-LECT" | perl -MPOSIX -lne 'while(/(.)/g) {$d = uc($1);
                                               if (grep /$d/, ("A".."Z")) {
                                                 $x = (grep /$d/, qw(S V Y Z)) ? 0 : 1;
                                                 $num .= (ceil((ord($d)-64)/3) + $x);
                                               } else {$num .= $1}
                                             }
                                             print $num'
1-800-265-5328
$
$
$
$ echo "1-800-col-lect" | perl -MPOSIX -lne 'while(/(.)/g) {$d = uc($1);
                                               if (grep /$d/, ("A".."Z")) {
                                                 $x = (grep /$d/, qw(S V Y Z)) ? 0 : 1;
                                                 $num .= (ceil((ord($d)-64)/3) + $x);
                                               } else {$num .= $1}
                                             }
                                             print $num'
1-800-265-5328
$
$
$
$ echo "1-800-ASK-USPS" | perl -MPOSIX -lne 'while(/(.)/g) {$d = uc($1);
                                               if (grep /$d/, ("A".."Z")) {
                                                 $x = (grep /$d/, qw(S V Y Z)) ? 0 : 1;
                                                 $num .= (ceil((ord($d)-64)/3) + $x);
                                               } else {$num .= $1}
                                             }
                                             print $num'
1-800-275-8777
$
$
$

tyler_durden

To input those characters you're using strings whether you know it or not :frowning:

It's off by one. Add 1 to the number before dividing by 3, I think.