| | |
Help with writing a bit of code
![]() |
•
•
Join Date: Sep 2005
Posts: 3
Reputation:
Solved Threads: 0
This may really simple to most people on here but i'm going crazy trying to work it out.
I was set a task by someone to write a bit of code in C to do the following:
1) Ask Operator to enter 2 things - a letter of the alphabet, (in upper or lower case) and a number.
2) Using the operator input - display to screen the letter of the alphabet - that many numbers in advance - in either upper or lower case depending on what the operator input.
3) After display to the screen, ask if they want to enter any more. If not - then exit.
E.g. - Operator enters letter f and number 3, display the character i.
Operator enters letter Z and number 5 display E.
Please help, i've tried loads of things but nothing i do seems to work, this is really doing my head in.
I'm very new to C, so any help would be most appreciated.
I was set a task by someone to write a bit of code in C to do the following:
1) Ask Operator to enter 2 things - a letter of the alphabet, (in upper or lower case) and a number.
2) Using the operator input - display to screen the letter of the alphabet - that many numbers in advance - in either upper or lower case depending on what the operator input.
3) After display to the screen, ask if they want to enter any more. If not - then exit.
E.g. - Operator enters letter f and number 3, display the character i.
Operator enters letter Z and number 5 display E.
Please help, i've tried loads of things but nothing i do seems to work, this is really doing my head in.
I'm very new to C, so any help would be most appreciated.
If you've already put together some code, post the code here (put it inside [code][/code] tags so it's readable), and we can help you find your problems.
Did we help you? Did we miss the point entirely? Update your thread and let us know.
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
•
•
Join Date: Aug 2005
Posts: 279
Reputation:
Solved Threads: 6
psionic,
I don't know C, but I put together the following "get started" code from the following website...
http://www.experts-exchange.com/Prog..._20483573.html
It won't compile I'm sure, but the basic idea is right. I don't know anything about printing upper vs lower case, but I know you can find a true/false value for isupper. Once you find that, you can then make the output match whether or not the input was upper vs. lower case. How to do this programmatically, is beyond me.
----------------------
--------
It's not the answer, it may not even be right, but hopefully it helps.
J_
I don't know C, but I put together the following "get started" code from the following website...
http://www.experts-exchange.com/Prog..._20483573.html
It won't compile I'm sure, but the basic idea is right. I don't know anything about printing upper vs lower case, but I know you can find a true/false value for isupper. Once you find that, you can then make the output match whether or not the input was upper vs. lower case. How to do this programmatically, is beyond me.
----------------------
C Syntax (Toggle Plain Text)
#include <stdio.h> int count; char alpha1; char alphabet[26]; main() /*load aphabet into an array*/ for(int i = 1; i <= 26; i++) { alphabet[1] = 'A'; // keep writing till.. alphabet[26] = 'Z'; } /*ask for input letter*/ printf("Please enter a letter: "); scanf("%c", &alpha1); /*ask for input number*/ printf("Please enter a number: "); scanf("%int", &count); /*find the letter's number from the array*/ while(!(alphabet[i] == alpha1)) { if( alphabet[i] == alpha1) position = i; else i++; } /*add the input number to the array number*/ i = i + count; /*print the array letter, assiciated with the array number*/ printf alphabet[i];
--------
It's not the answer, it may not even be right, but hopefully it helps.
J_
Last edited by Dave Sinkula; Sep 21st, 2005 at 1:25 pm. Reason: Added [code][/code] tags.
•
•
Join Date: Aug 2005
Posts: 279
Reputation:
Solved Threads: 6
psionic,
I'm sorry, this completely slipped my mind.
Just before: printf alphabet[i]; you'll want to check for numbers > 26, because keep in mind the addition can end up being larger than 26 and no array values are above that.
This is simple, however, not truly functional. It won't work if you have a value larger than 52. So for this, you have to do some math.
I'm going to write it in psuedo code, becuase I really can't write it in C.
ok so i didn't write this next part in because i didn't want to confuse you any more than I already am.
You'll need to have the program enter into a for loop where it keeps decrementing the > 52 if statement. Meaning if " i " was originaly 100, the 1st go around would end with 78. You need to again compare 78 with 52. Since it is larger, re-apply the calculation to get it to a value lower than 27.
No wonder why I'm not a programmer.
J_
I'm sorry, this completely slipped my mind.
Just before: printf alphabet[i]; you'll want to check for numbers > 26, because keep in mind the addition can end up being larger than 26 and no array values are above that.
This is simple, however, not truly functional. It won't work if you have a value larger than 52. So for this, you have to do some math.
I'm going to write it in psuedo code, becuase I really can't write it in C.
C Syntax (Toggle Plain Text)
{ if ( i > 52 ) /*check for numbers larger than 52 (we'll use 58)*/ d = i / 26; /* d = 58 / 26 >> d = 2.2307...*/ (some function to remove the decimal)/* d = 2 */ e = 26 * d; /* e = 26 * 2 >> e = 52 */ i = i - e /* i = 58 - 52 >> i = 6 */ printf alphabet[i]; /* " f " */ else if (i > 26) /* perhaps the number is less than 52, yet larger than 26*/ i = 26 - i /* simple subtraction */ printf alphabet[i]; else /* less than or equal to 26 */ printf alphabet[i]; }
ok so i didn't write this next part in because i didn't want to confuse you any more than I already am.
You'll need to have the program enter into a for loop where it keeps decrementing the > 52 if statement. Meaning if " i " was originaly 100, the 1st go around would end with 78. You need to again compare 78 with 52. Since it is larger, re-apply the calculation to get it to a value lower than 27.
No wonder why I'm not a programmer.
J_
•
•
Join Date: Sep 2005
Posts: 3
Reputation:
Solved Threads: 0
Thanks to J_Search for all your help,
So far i've writte the below, and it all seems to work
All i need to add now is the ability for it to accept uppercase letters, also to loop if the user wants to have another go. If anyones got some ideas on how to get it to accept uppercase from the user input, i would would be most grateful .
Thanks again
So far i've writte the below, and it all seems to work
C Syntax (Toggle Plain Text)
#include <stdio.h> int count; char alpha1; int position = 0; int i = 0; main() { /*load alphabet into an array*/ char alphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; /*ask for input letter*/ printf("Please enter a letter: "); scanf("%c", &alpha1); /*ask for input number*/ printf("Please enter a number: "); scanf("%int", &count); /*find the letter's number from the array*/ for( i = 0; i < 26 && alphabet[i] != alpha1; i++); /*Check if user has not entered a letter on first question*/ if(i >= 26) { /* Output message if user has not inputted a letter, then exits program*/ printf("\n You have not entered a letter"); } else { /*add the input number to the array number*/ i += count; i %= 26 ; /*print the array letter, associated with the array number*/ printf ("The Letter you have asked for is %c\n",alphabet[i]); } return 0; }
All i need to add now is the ability for it to accept uppercase letters, also to loop if the user wants to have another go. If anyones got some ideas on how to get it to accept uppercase from the user input, i would would be most grateful .
Thanks again
•
•
Join Date: Sep 2005
Posts: 3
Reputation:
Solved Threads: 0
I have one problem left that i really hoping someone can help me with.
I need to get the program to ask the user if they want another turn, then if they enter Y the program loops, if they enter N then it exits.
Everyhting i try fails.
If anyone can help please reply.
what i've got so far is below.
I need to get the program to ask the user if they want another turn, then if they enter Y the program loops, if they enter N then it exits.
Everyhting i try fails.
If anyone can help please reply.
what i've got so far is below.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <ctype.h> int count; char alpha1; int position = 0; int i = 0; int token1 = 0; char go[2]; int flag = 0; main() { do { /*load alphabet into an array*/ char alphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; /*ask for input letter*/ printf("Please enter a letter: "); scanf("%c", &alpha1); /*ask for input number*/ printf("Please enter a number: "); scanf("%int", &count); if(isupper(alpha1)) { alpha1 = tolower(alpha1); token1 = 1; } /*find the letter's number from the array*/ for( i = 0; i < 26 && alphabet[i] != alpha1; i++); /*Check if user has not entered a letter on first question*/ if(i >= 26) { /* Output message if user has not inputted a letter, then exits program*/ printf("\n You have not entered a letter"); } else { /*add the input number to the array number*/ i += count; i %= 26 ; if(token1 == 1) alphabet[i] = toupper(alphabet[i]); /*print the array letter, associated with the array number*/ printf ("The Letter you have asked for is %c\n",alphabet[i]); } do { printf("\nAnother go (Y/N)?"); scanf("%s", go); // go = toupper(go); } while ((go !='Y') && (go !='N')); } return 0; }
•
•
Join Date: Jul 2005
Posts: 11
Reputation:
Solved Threads: 0
That seems a bit overkill, why not just cast the character to an integer and add the number then recast back to character? This also deals with the idea of outputting in the same case as was input. Of course you could also put in a check to make sure the number entered is under 25 and that the output is not greater than Z.
[PHP]
#include <stdlib>
#include <stdio.h>
int main(int argc, char* argv[])
{
char letter;
int number;
int charNum;
printf("Enter letter: ");
scanf("%c", &letter);
printf("Enter number: ");
scanf("%d", &number);
charNum = (int)letter;
charNum = charNum + number;
letter = (char)charNum;
printf ("The Letter you have asked for is %c\n", letter);
system("PAUSE");
return 0;
}[/PHP]
[PHP]
#include <stdlib>
#include <stdio.h>
int main(int argc, char* argv[])
{
char letter;
int number;
int charNum;
printf("Enter letter: ");
scanf("%c", &letter);
printf("Enter number: ");
scanf("%d", &number);
charNum = (int)letter;
charNum = charNum + number;
letter = (char)charNum;
printf ("The Letter you have asked for is %c\n", letter);
system("PAUSE");
return 0;
}[/PHP]
>That seems a bit overkill
It does at first glance.
>why not just cast the character to an integer and add the number then recast back to character?
Well, first because there's no need to cast, and second because letters in the basic character set are not required to be consecutive. So your code could (and would!) fail mysteriously if run on a system that uses, say, EBCDIC.
It does at first glance.
>why not just cast the character to an integer and add the number then recast back to character?
Well, first because there's no need to cast, and second because letters in the basic character set are not required to be consecutive. So your code could (and would!) fail mysteriously if run on a system that uses, say, EBCDIC.
I'm here to prove you wrong.
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: URGENT!! atof, strtof, strod ...drivin me nuts!!
- Next Thread: help with null terminated strings
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






