string upper case

This is a discussion on string upper case within the C++ Programming forums, part of the General Programming Boards category; //simple program, supposed to turn everyother character //in the array to upercase #include <iostream> #include <iomanip> #include <cstdlib> #include <string> ...

  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, 01:11 AM
  3. Xmas competitions
    By Salem in forum Contests Board
    Replies: 88
    Last Post: 01-03-2004, 01:08 PM
  4. Convert a string to upper case?
    By jpp1cd in forum C Programming
    Replies: 2
    Last Post: 12-12-2002, 06:49 PM
  5. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM

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