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


 
Thread Tools Search this Thread
Top Forums Programming I don't know how to replace input char with appropriate integer
# 1  
Old 10-07-2011
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

Last edited by solaris_user; 10-07-2011 at 10:33 AM..
# 2  
Old 10-07-2011
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.
# 3  
Old 10-08-2011
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:
Quote:
1-800-18808512134511376134511136-16795404134511152-17037708-17873307-1847851041-17330988134548607-16795404134511136
# 4  
Old 10-08-2011
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:

Code:
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);
}

# 5  
Old 10-09-2011
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 Smilie

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

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

But anyway thanks for help
# 6  
Old 10-09-2011
Quote:
Originally Posted by solaris_user
...
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
...
Code:
$
$
$
$ 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
# 7  
Old 10-09-2011
Quote:
Originally Posted by solaris_user
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 Smilie
To input those characters you're using strings whether you know it or not Smilie
Quote:
Now about your program, I compiled it and works but don't works correctly.

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

But anyway thanks for help
It's off by one. Add 1 to the number before dividing by 3, I think.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX Desktop Questions & Answers

awk a integer and replace it minus X

hey, i have a list of devices that looks like so: VolumeTask(DeviceName(DeviceName(DeviceNameInfo(MultiPathType_UID(Option<GUDeviceID>(GUDeviceID(ulonglong(358271365120608989UL))),Option<ulong>())))),2098560),... (5 Replies)
Discussion started by: boaz733
5 Replies

2. Shell Programming and Scripting

Using IF statements with maths where the input is not an integer

Hi All I've made a few scripts which using GDAL extract the value of a pixel within a given raster. The purpose is to work out the combine value of every pixel. I thought there may have been an easier way to do this but alas! The code below extracts the pixel value at position X Y. The... (3 Replies)
Discussion started by: StudentFitz
3 Replies

3. UNIX for Dummies Questions & Answers

Check if input is an integer or a floating point?

Hiii I actually intent to check the integer or floating point number input by user i.e. 23, 100, 55.25, 12.50 ..etc. However, when someone input strings or alpha character, my program has to show invalid input.!! Is there any Unix shell script syntax can help me to check ? Thanking you (2 Replies)
Discussion started by: krishnampkkm
2 Replies

4. UNIX for Dummies Questions & Answers

Input of char 's' is not possible anymore

Hello, I have a big problem and no idea how to solve it. I was looking up commands in /bin with 'man' as I found 'sh'. In mistake I started the command. Now after that I can not input the character 's' in shell anymore! Even after restarting system the problem is still there. Mysterious... (7 Replies)
Discussion started by: daWonderer
7 Replies

5. Programming

How i can read a long integer from standar input and a string with as many characters as specified..

how i can read a long integer from standar input and a string with as many characters as specified in the number? i thing that i must use the read command ofcourse.... (6 Replies)
Discussion started by: aintour
6 Replies

6. Programming

to compare two integer values stored in char pointers

How can I compare two integer values which is stored in char pointers? suppose I have char *a and char *b having values 10 and 20. how can i find the shorter value? (1 Reply)
Discussion started by: naan
1 Replies

7. UNIX for Advanced & Expert Users

test the string is char or integer

How will test the string contains numeric character or alphabet, is there any script to test ? (10 Replies)
Discussion started by: rajesh08
10 Replies

8. Shell Programming and Scripting

How to replace any char with newline char.

Hi, How to replace any character in a file with a newline character using sed .. Ex: To replace ',' with newline Input: abcd,efgh,ijkl,mnop Output: abcd efgh ijkl mnop Thnx in advance. Regards, Sasidhar (5 Replies)
Discussion started by: mightysam
5 Replies

9. Shell Programming and Scripting

Replace floating-point by integer in awk

Hi, I am trying to write a script to extract multiple sets of data from a chemistry output file. The problem section is in the following format... Geometry "geometry" -> "geometry" 1 Pd 46.0000 -0.19290971 0.00535260 0.02297606 2 P ... (7 Replies)
Discussion started by: smadonald1
7 Replies

10. Shell Programming and Scripting

Don't show keyboard input on terminal

I am developing a script that will run with '/bin/ksh' shell. The script is intended to receive a password by keyboard input, but for security reasons I would like to hide what the user is typing. The keyboard input is being caught by 'read' command. exmaple : echo "Please type your new... (1 Reply)
Discussion started by: marianor31
1 Replies
Login or Register to Ask a Question