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.

Recommended Answers

All 11 Replies

If you've already put together some code, post the code here (put it inside code block so it's readable), and we can help you find your problems.

psionic,

I don't know C, but I put together the following "get started" code from the following website...
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_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.

----------------------

#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_

psionic,

I'm sorry, this completely slipped my mind.

Just before: printf alphabet; 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.

{

 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_

Thanks to J_Search for all your help,

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

#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

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.

#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;
}

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.

#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;
}

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

tell me how to use in bitwise operator in date program

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.

ya...Here i have included a file in which i used circular queue to find the correct alphabet....

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 AM ALSO A BEGINNER,TRY MY CODE

void main()
{
    int n,c;char a;
    scanf("%c%d",&a,&n);//a is the character and n is the value by whch charactr is incremented
    c=a+(n%26);
    if(c>122 || (c>90 && (int)a<91))
        c=c-26;
    printf("%c ",c);
    getch();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.