So i need to correct the capitalization of the string so that every first letter would be Upper, here's what i've done (not sure if i'm using the Substring correctly):
1 2 3 4 5 6 7 8 9 10 11 12
for (int i = 0; i < amount; i++)
{
cities [i].ToLower ();
upper = cities [i];
for (int j = 0; j < cities [i].Length; j++)
{
upper.Substring (0).ToUpper ();
if (upper.Substring (j) == " ")
upper.Substring (j + 1).ToUpper ();
}
cities [i] = upper;
}
but it does nothing, were is my mistake?(maybe the usage of substring)
#include <iostream>
using std::endl;
using std::cout;
using std::cin;
#include <cctype>
using std::isalpha;
using std::tolower;
using std::toupper;
void processArray(char *const, constint); // function prototype
int main()
{
char data[] = { "tEST TesT TEST" };
int size = sizeof(data) / sizeof(char); //size of array in bits divided by normal size of
// a char resulting in the size of array
processArray(data, size);
cout << data; //print array
cin.ignore(255, '\n'); // prevent program from closing down unexpected
return 0;
}
void processArray(char *const data, constint size) //using pointer "*" to pass the array in function
{
bool isFirst = true; // aux bool to help us find the first letter
for (int elem = 0; elem < size; elem++)
{
if (isalpha(data[elem])) // if elem in array is letter
{
if (isFirst == true)
{
data[elem] = toupper(data[elem]); //set upper case in case its first letter
isFirst = false; //we dont need upper case for now
}
else
data[elem] = tolower(data[elem]); // set lower case if its not first letter
}
else
isFirst = true; // if elem in array is not a letter we start a new word
}
}
Enjoy free homework , i was in mood for some coding, its not the best, just something from the tip of my head