Thread: string upper case

  1. #1
    Shadow12345
    Guest

    string upper case

    //simple program, supposed to turn everyother character
    //in the array to upercase


    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <string>

    using namespace std;

    int main()
    {
    int x;
    char *array = new char[20];
    cout << "Enter a string before i kill you" << endl;
    cin.getline(array, 20);
    cout << "You have entered " << array << endl;

    x = sizeof(array) - 1;
    cout << "The size of your array is " << x << endl;
    for(x = 0; x < 20; x += 2) //while x less than 20 add 2
    strupr(array[x]); //make character at location
    return 0; //x uppercase
    }

  2. #2
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Here is a different, more standard version:

    Code:
    #include <iostream> 
    #include <cstdlib> 
    #include <string> 
    
    using namespace std; 
    
    int main() 
    { 
       char *array = new char[20]; 
       
       cout << "Enter a string: "; 
       cin.getline(array, 20); 
    	
       cout << "You have entered " << array << endl; 
       cout << "The size of your array is " << strlen(array) << " characters" << endl; 
    	
       for(int x = 0; x < 20; x += 2)		
           array[x] = toupper(array[x]);	 
    
       cout << "You have entered " << array << endl; 
    
       delete [] array;
    
       return 0; 
    }
    You should/could use the standard toupper() function and the strlen() makes more sense to use in this situation. Also you forgot to deallocate the memory you allocated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Xmas competitions
    By Salem in forum Contests Board
    Replies: 88
    Last Post: 01-03-2004, 02:08 PM
  4. Convert a string to upper case?
    By jpp1cd in forum C Programming
    Replies: 2
    Last Post: 12-12-2002, 07:49 PM
  5. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM