Mixed Letter Converter

This is a discussion on Mixed Letter Converter within the C++ Programming forums, part of the General Programming Boards category; i have writen a program that asks the user to input letters and it will change it to upper case. ...

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    Mixed Letter Converter

    i have writen a program that asks the user to input letters and it will change it to upper case. i have done this by using the common way of "toupper". i then got told that i cant have any letter manipulation or string commands to do this. can anyone help as to do this?

    here is the code i had before...

    Code:
    #include <iostream>
    #include <iomanip>
    #include <ctype.h>
    
    using namespace std;
    
    int main()
    
     {
    
      char input[25];
      int i = 0;
    
      cout <<"Please enter a word of mixed letters eg - MiXeD LeTtErS";
      cin >> input;
        for(;;)
         {
        
        if (input[i] == '\0') break;
        
        else
            {
            input[i] = toupper(input[i]);
            }
            
        i++;
     }
     
        cout <<input;
    
    system("pause");
    }

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Oh dear ...

    You've got a really stupid assignment there. "Can't use toupper()" must be the most stupid requirement I've ever seen.

    If it were me, I'd tell the teacher that it is, unfortunately, impossible to do this reliably since I cannot possibly know every character set used by all the platforms my program might be compiler under, unlike the writers of that platform's CRT, who would implement toupper() for me to use, if the idiotic requirement didn't prevent me from using it.

    Ahem. But your teacher probably won't be too happy if you do that, so here's a poor man's toupper() that relies on the character set having these properties: a through z have consecutive character values and A to Z have consecutive character values, and char is signed by default or the character values of lower-case letters are higher than those of upper-case letters.
    The platform you're working on will most likely fulfill these criteria.
    Code:
    char poormanstoupper(char c)
    {
      if(c >= 'a' && c <= 'z') { // If it is a lower-case letter
        return c - ('a' - 'A'); // Subtract the offset from the upper-case to the lower-case letters.
      } else {
        return c; // Non-lowercase letters get returned unchanged.
      }
    }
    Substitute the standard toupper in your code with this solution and replace your infinite for-loop-with-break with a proper while loop - it's actually less code and easier to understand if you do. Then replace operator >> by cin.getline, which will not stop on whitespace and which you can pass the buffer size to prevent overflow.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Ok, here are some pointers about writing code in green highlights

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
    
    	std::string input = "MiXtUres"; // use std::string, unless you aren't allowed to
    	std::string::size_type i = 0;
    	
    	for( i=0; i<input.size(); i++ )	// A more conventional way of doing the loop. It has parameters! Why not use them?
    	{
    		// Your toupper solution.
    	}
    
    	std::cout<< input;
    
    	systemp ("pause"); // Look into other ways in the FAQ as to how to achieve an equivalent effect!
    
    	return 0; // Main returns an int
    }
    Does the word have to be stored in a variable? Or can you just print out the letter as it goes through the loop?

  4. #4
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    Yeah dont think he would be too impressed so i best do it, lol.

    the word or letter has to be stored in an array that we have to manage ourselves.

    What would i need to change to get it to read out a full word and then convert it?

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    i then got told that i cant have any letter manipulation or string commands to do this. can anyone help as to do this?
    This means good old if/else with a loop
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    yeah i knw that but unsure how to do this in a loop, i am new to programming so need a few pointers n examples n stuff to learn "The only way to program is to program"

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Does this help?
    Code:
    #include <iostream>
    int main(){
    	char mean[256];
    	std::cin.getline(mean,256,'\n');
    	for(int n=0;mean[n]!='\0';n++){
    		if(mean[n]>=97 && mean[n]<=122){
        		mean[n]-=32;
    		}
    	}
    	std::cout<<mean<<std::endl;
    	system("PAUSE");
    }
    Edit: fixed 123 to 122 and added endl to the end of output.
    Last edited by maxorator; 10-07-2006 at 02:39 PM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  8. #8
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    yeah that works, thanx alot for the help!!!!

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Lower case letters are 97-122 in ASCII table and upper case letters are exactly 32 less (65-90).
    So if the letter decimal is from 97 to 122 we can just subtract 32.
    Last edited by maxorator; 10-07-2006 at 02:36 PM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    And it's still better to use the character literals like I did in my code above.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    i then got told that i cant have any letter manipulation or string commands to do this.
    Notice that ?
    My solution used no functions (except the main), structures, classes nor extra datatypes whatsoever for the loop.
    Think of it like this. You must convert mixed thing to upper case without including ANY files.
    Code:
    int main(){
    	char mean[]={"MixED ConTenT"};
    	for(int n=0;mean[n]!='\0';n++){
    		if(mean[n]>=97 && mean[n]<=122){
        		mean[n]-=32;
    		}
    	}
    }
    Last edited by maxorator; 10-08-2006 at 06:57 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I assumed that referred to letter manipulation commands from the CRT. Not being allowed to work with character literals would reach a new level of dumb I wouldn't expect from even the worst C++ course.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I like doing things like that (without using functions), because it's fun and I need to actually think how to do it.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Sure, it can be fun. It's also hard to read and maintain, so it shouldn't be done outside small, isolated fun programlets that you do in your spare time.

    (Although personally, I don't consider trying to remember what ASCII code represents which character fun at all.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This code doesn't work
    Code:
    char mean={"MixED ConTenT"}; //invalid conversion from `const char*' to `char'
    You probably meant:
    Code:
    char mean[]="MixED ConTenT";

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I ...
    By geekrockergal in forum C Programming
    Replies: 21
    Last Post: 02-07-2009, 09:40 AM
  2. counting letter occurences in a string
    By pjr5043 in forum C++ Programming
    Replies: 35
    Last Post: 05-05-2008, 09:18 PM
  3. Advice requested, Code makes sense to me, not compiler
    By andrew.bolster in forum C Programming
    Replies: 53
    Last Post: 01-06-2008, 12:44 PM
  4. help using strings and mapping
    By trprince in forum C Programming
    Replies: 29
    Last Post: 12-01-2007, 03:01 PM
  5. Big Letter became small letter
    By cogeek in forum C Programming
    Replies: 27
    Last Post: 12-13-2004, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21