Can i get help with C# ( ToUpper() )?

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)
Last edited on
closed account (E3h7X9L8)
i dont really understand what you need to do ? can you give us a example?

as i understood you have to make upper case every first letter of every word and rest of the letter to make them lower case

ex: tEST TesT TEST >> Test Test Test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#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, const int); // 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, const int 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
Strings are immutable in C# so Substring and ToUpper can't modify the string. Instead they return a new string.
Topic archived. No new replies allowed.