944,076 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2347
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 21st, 2005
0

Help with writing a bit of code

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
psionic is offline Offline
3 posts
since Sep 2005
Sep 21st, 2005
0

Re: Help with writing a bit of code

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.
Reputation Points: 38
Solved Threads: 25
Posting Shark
chrisbliss18 is offline Offline
902 posts
since Aug 2005
Sep 21st, 2005
0

Re: Help with writing a bit of code

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.

----------------------
  1. #include <stdio.h>
  2.  
  3. int count;
  4. char alpha1;
  5. char alphabet[26];
  6.  
  7. main()
  8.  
  9. /*load aphabet into an array*/
  10. for(int i = 1; i <= 26; i++)
  11. {
  12. alphabet[1] = 'A';
  13. // keep writing till..
  14. alphabet[26] = 'Z';
  15. }
  16.  
  17. /*ask for input letter*/
  18. printf("Please enter a letter: ");
  19. scanf("%c", &alpha1);
  20.  
  21. /*ask for input number*/
  22. printf("Please enter a number: ");
  23. scanf("%int", &count);
  24.  
  25. /*find the letter's number from the array*/
  26. while(!(alphabet[i] == alpha1))
  27. {
  28. if( alphabet[i] == alpha1)
  29. position = i;
  30. else
  31. i++;
  32. }
  33.  
  34. /*add the input number to the array number*/
  35. i = i + count;
  36.  
  37. /*print the array letter, assiciated with the array number*/
  38. 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.
Reputation Points: 27
Solved Threads: 6
Posting Whiz in Training
J_Search is offline Offline
284 posts
since Aug 2005
Sep 21st, 2005
0

Re: Help with writing a bit of code

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.
  1. {
  2.  
  3. if ( i > 52 ) /*check for numbers larger than 52 (we'll use 58)*/
  4.  
  5. d = i / 26; /* d = 58 / 26 >> d = 2.2307...*/
  6.  
  7. (some function to remove the decimal)/* d = 2 */
  8.  
  9. e = 26 * d; /* e = 26 * 2 >> e = 52 */
  10.  
  11. i = i - e /* i = 58 - 52 >> i = 6 */
  12.  
  13. printf alphabet[i]; /* " f " */
  14.  
  15. else if (i > 26) /* perhaps the number is less than 52, yet larger than 26*/
  16.  
  17. i = 26 - i /* simple subtraction */
  18.  
  19. printf alphabet[i];
  20.  
  21. else /* less than or equal to 26 */
  22.  
  23. printf alphabet[i];
  24.  
  25. }

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_
Reputation Points: 27
Solved Threads: 6
Posting Whiz in Training
J_Search is offline Offline
284 posts
since Aug 2005
Sep 22nd, 2005
0

Re: Help with writing a bit of code

Thanks to J_Search for all your help,

So far i've writte the below, and it all seems to work

  1. #include <stdio.h>
  2.  
  3. int count;
  4. char alpha1;
  5. int position = 0;
  6. int i = 0;
  7.  
  8. main()
  9. {
  10. /*load alphabet into an array*/
  11. 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'};
  12.  
  13. /*ask for input letter*/
  14. printf("Please enter a letter: ");
  15. scanf("%c", &alpha1);
  16.  
  17. /*ask for input number*/
  18. printf("Please enter a number: ");
  19. scanf("%int", &count);
  20.  
  21. /*find the letter's number from the array*/
  22. for( i = 0; i < 26 && alphabet[i] != alpha1; i++);
  23. /*Check if user has not entered a letter on first question*/
  24. if(i >= 26)
  25. { /* Output message if user has not inputted a letter, then exits program*/
  26. printf("\n You have not entered a letter");
  27. }
  28.  
  29. else
  30. {
  31. /*add the input number to the array number*/
  32. i += count;
  33. i %= 26 ;
  34.  
  35. /*print the array letter, associated with the array number*/
  36. printf ("The Letter you have asked for is %c\n",alphabet[i]);
  37. }
  38. return 0;
  39. }

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
psionic is offline Offline
3 posts
since Sep 2005
Sep 26th, 2005
0

Re: Help with writing a bit of code

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.

  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. int count;
  5. char alpha1;
  6. int position = 0;
  7. int i = 0;
  8. int token1 = 0;
  9. char go[2];
  10. int flag = 0;
  11.  
  12. main()
  13. {
  14. do
  15. {
  16. /*load alphabet into an array*/
  17. 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'};
  18.  
  19. /*ask for input letter*/
  20. printf("Please enter a letter: ");
  21. scanf("%c", &alpha1);
  22.  
  23. /*ask for input number*/
  24. printf("Please enter a number: ");
  25. scanf("%int", &count);
  26.  
  27. if(isupper(alpha1))
  28. {
  29. alpha1 = tolower(alpha1);
  30. token1 = 1;
  31. }
  32.  
  33. /*find the letter's number from the array*/
  34. for( i = 0; i < 26 && alphabet[i] != alpha1; i++);
  35. /*Check if user has not entered a letter on first question*/
  36.  
  37. if(i >= 26)
  38. {
  39. /* Output message if user has not inputted a letter, then exits program*/
  40. printf("\n You have not entered a letter");
  41. }
  42.  
  43. else
  44. {
  45. /*add the input number to the array number*/
  46. i += count;
  47. i %= 26 ;
  48.  
  49.  
  50. if(token1 == 1)
  51. alphabet[i] = toupper(alphabet[i]);
  52.  
  53. /*print the array letter, associated with the array number*/
  54. printf ("The Letter you have asked for is %c\n",alphabet[i]);
  55.  
  56. }
  57.  
  58. do
  59. {
  60.  
  61. printf("\nAnother go (Y/N)?");
  62. scanf("%s", go);
  63. // go = toupper(go);
  64. }
  65.  
  66. while ((go !='Y') && (go !='N'));
  67. }
  68.  
  69. return 0;
  70. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
psionic is offline Offline
3 posts
since Sep 2005
Sep 26th, 2005
0

Re: Help with writing a bit of code

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]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ramcfloyn is offline Offline
10 posts
since Jul 2005
Sep 26th, 2005
0

Re: Help with writing a bit of code

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Aug 14th, 2010
0
Re: Help with writing a bit of code
tell me how to use in bitwise operator in date program
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vijay143 is offline Offline
2 posts
since Jul 2010
Aug 14th, 2010
0
Re: Help with writing a bit of code
Vijay, you need to stop posting in an old thread - your post will be pretty much ignored and considered old as well.

Or posters will answer Psionics questions, and not yours.

Start a new thread, and be as specific as you can. Post the code that you're working with so we can see, as well as read, what you're doing, and what your trying to do.

Your new problem needs a new thread, and more information too.
Reputation Points: 416
Solved Threads: 181
Nearly a Posting Virtuoso
Adak is offline Offline
1,463 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Help accessing nested structures
Next Thread in C Forum Timeline: linear list





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC