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