convert lower case letter to upper case letter by using C

convert lower case letter to upper case letter by using C

Page 1 of 1

8 Replies - 27504 Views - Last Post: 29 September 2008 - 11:54 PM Rate Topic: -----

#1 nora.erdm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-September 08

convert lower case letter to upper case letter by using C

Post icon  Posted 26 September 2008 - 09:48 AM

hi
this is my 1st homework for C programming class

I need to write a program that reads a word composed of 4 lower case character and print it in upper case characters.

help me, plz;

here is some additional things:

1. read a word composed of 4 character using the following statement.
scanf("%c%c%%c", ......);
2. convert each character into uppercase character.
hint) 'A'='a'-0*20;// 'A'=0841, 'a'=0*61 ..etc
3. print out the result using the following statement
printf("%c%c%c%c", ....);

thank u :) waiting your helps

Is This A Good Question/Topic? 0
  • +

Replies To: convert lower case letter to upper case letter by using C

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: convert lower case letter to upper case letter by using C

Posted 26 September 2008 - 10:43 AM

So, show us what *you* have done. This has been pretty clearly described for you, so it shouldn't be difficult.
Was This Post Helpful? 0
  • +
  • -

#3 nora.erdm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-September 08

Re: convert lower case letter to upper case letter by using C

Posted 26 September 2008 - 06:56 PM

View PostJackOfAllTrades, on 26 Sep, 2008 - 10:43 AM, said:

So, show us what *you* have done. This has been pretty clearly described for you, so it shouldn't be difficult.


I am writing like this:

#include <stdio.h>
#include <stdlib.h>
/* This program reads a word composed of 4 lower case characters and print it in upper case characters*/
char LowerToUpper (char n);
int main ()
{
//declare variables
char a;// the word that consists from 4 chars
char b;
char c;
char d;
//Read the word
printf("This program reads a word composed of 4 lower case \ncharacters and print it in upper case characters.\n");
printf("Input a word composed of 4 lower case charactere: \n");
scanf("%c%c%c%c", &a, &b, &c, &d);
//convert each characters into uppercase characters
printf()

//print out the results
printf("this is: %c%c%c%c", a, b, c, d);

system("PAUSE");
return 0;
}
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: convert lower case letter to upper case letter by using C

Posted 26 September 2008 - 08:13 PM

Here is a hexadecimal ASCII chart.

Here is a sample:
int main(int argc, char **args)
{
    const char *test = "test";
	int upperCase[4];
	for (int i = 0; i < sizeof(test)/sizeof(test[0]); ++i)
		upperCase[i] = test[i] - 0x20;

	for (int i = 0; i < sizeof(upperCase)/sizeof(upperCase[0]); ++i)
		printf("%c", upperCase[i]);

	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#5 nora.erdm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-September 08

Re: convert lower case letter to upper case letter by using C

Posted 27 September 2008 - 12:35 AM

thank you anyway. i dont know about for and sizeof.

and I wrote in very easy way. thanks
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,591
  • Joined: 16-October 07

Re: convert lower case letter to upper case letter by using C

Posted 27 September 2008 - 04:00 AM

View Postnora.erdm, on 27 Sep, 2008 - 03:35 AM, said:

i dont know about for and sizeof.

and I wrote in very easy way. thanks


Given the problem, you shouldn't need sizeof. A for loop would be best, but we'll leave that for the future.

So, let's keep it simple.

#include <stdio.h>

char toUpper(char ch) {
	// given 'A'= 'a' - 32
	return ch-32;
}

int main () {
	// the word that consists from 4 chars 
	// plus a terminating character
	// this is a string
	char word[5]; 

	printf("Input a word composed of 4 lower case characters: \n");
	scanf("%s", word); // here, we're reading a string
	
	//convert each characters into uppercase characters
	word[0] = toUpper(word[0]);  // apply our fuction
	word[1] = toUpper(word[1]);  // apply our fuction
	word[2] = toUpper(word[2]);  // apply our fuction
	word[3] = toUpper(word[3]);  // apply our fuction
	
	printf("this is: %s", word);
	return 0;
}



Hope this is simple enough and makes sense.
Was This Post Helpful? 0
  • +
  • -

#7 nora.erdm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-September 08

Re: convert lower case letter to upper case letter by using C

Posted 27 September 2008 - 05:41 AM

hey Baavgai
thats nice coding.

thanks
Was This Post Helpful? 0
  • +
  • -

#8 gelz_143  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-September 08

Re: convert lower case letter to upper case letter by using C

Posted 29 September 2008 - 07:11 AM

is it something like this?

#include<stdio.h>
#include<ctype.h>

int main()
{
   char word;
   int x;
   clrscr();
   printf("Enter a word: ");

   for(x=1; x<=4; x++)
   {
	  scanf("%c", &word);
	  word = toupper(word);
	  printf("%c", word);
   }

   getch();
}


Was This Post Helpful? 0
  • +
  • -

#9 RedSonja  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 172
  • Joined: 04-September 08

Re: convert lower case letter to upper case letter by using C

Posted 29 September 2008 - 11:54 PM

ToUpper does not return an error code, so you should add a check that the characters entered really are lower case chars. Else if your user enters '&' he will get a control code back, a bit of a pain.

Something like if c>='a' && c<='z'

If you want to be clever you can write your own version of ToUpper. Quite easy, since in the ASCII table a-z is one column to the right of A-Z. Then you can make it do intelligent things with chars not between a and z, like returning a *.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1