chokosc
15 years ago
9 months ago
1,041
One of the subjects in the big subject I am taking this year is Algorithms and we just started using the IF command.

For our first project we need to think of a problem and solve it. I decided to go as close as possible to what we did in class. What we did in class is take a word, change it's first letter to a capital letter and then put the capital letter back. The problem is that it was only the first letter and not the rest of the word.

So I decided to try and get the whole word in and this is what I got so far:

start:
Console.WriteLine("Please enter a word below:";
Console.WriteLine("";
string str = Console.ReadLine();
char char1;
if (str[0] >= 97)
{
char1 = (char)(str[0] - 32);
}
else
{
char1 = (char)(str[0] + 32);
}


char char2 = (char)(str[1]);
char char3 = (char)(str[2]);
char char4 = (char)(str[3]);
char char5 = (char)(str[4]);

Console.WriteLine("";
Console.Write(char1);
Console.Write(char2);
Console.Write(char3);
Console.Write(char4);
Console.WriteLine(char5);

goto start;

The problem with that is that it has to be a 5 letter word. if it is lower than that, it just gives me an error and if it is more than that it doesn't finish the word.

So my question is: is there a way to use if and tell it how many chars we need, depending on how many letters there are? So basically:

if amount of letters = 2

char char2 = (char)(str[1]);

if amount of letters = 3

char char2 = (char)(str[1]);
char char3 = (char)(str[2]);

and so on.

Thanks for any help!
Phoenix Arrow
16 years ago
2 years ago
825
Oh lordy. It's been a while since I've used C#. Is there not some sort of get string length sort of function? I can't remember if that was build in or something I made. I think it's built in, but I couldn't tell you the name of it. Might just be char.length actually.

You'd need to make char2, char3, char4 etc an array though. Then make a loop which goes as long as char.length - 1.
chokosc
15 years ago
9 months ago
1,041
I will look into that one, thanks
chokosc
15 years ago
9 months ago
1,041
By Phoenix Arrow | Permalink | On 25 October 2012 - 21:36 PM
You'd need to make char2, char3, char4 etc an array though. Then make a loop which goes as long as char.length - 1.


What do you mean?
Phoenix Arrow
16 years ago
2 years ago
825
Well, if you don't know how long the string is going to be, you're not going to be able to know how many characters you need. If you make it an array then you can make a for loop which doesn't need to know that. It would also make your code like 4 times shorter than it currently is.
chokosc
15 years ago
9 months ago
1,041
And how would I go about doing that? I have two problems here. 1. We haven't exactly used array. 2. I am learning all of this in half Hebrew and half English

I found this website that explains arrays but I am not sure which to use and how.

http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx
Phoenix Arrow
16 years ago
2 years ago
825
Oh, so have you literally just started? Have you done for loops yet? So if you had your string it would be something like umm.

char[] arr;
int length = str.length;
length--;

for(int i = 0; i <= length; i++)
{
    arr[i] = str[i];
    if(i == 0)
        -Then here you'd put whatever makes the first character capital for arr[0]-
    Console.Write(arr[i];
}
I think that's about write. But I haven't done any programing since May and that was web stuff. I haven't done any C++ since Jan 2011 and I haven't done any C# since a year before that because I do more IT support now than programming so I'm really rusty and was bad at it to begin with. I also don't really know what you've done before or what you're meant to be using. If you're stuck, talk to your lecturer. If you're stuck and you don't want to ask your lecturer, try asking on stackoverflow.com. Make sure you make it clear you're a beginner though or you might get a bunch of shit back which makes no sense.
chokosc
15 years ago
9 months ago
1,041
Haha, started this year and we only have 3 periods a week so not much has been done! So in the thing that you did, do I just need to add the

char1 = (char) (str[0]-32)
in the Console.WriteLine?
chokosc
15 years ago
9 months ago
1,041
anything with .length after it is giving me an error :\
Phoenix Arrow
16 years ago
2 years ago
825
I assume it's the whole if/else statement you wrote.
Phoenix Arrow
16 years ago
2 years ago
825
Oh. Maybe it's a capital L.
chokosc
15 years ago
9 months ago
1,041
I will give this one last try Also posted this on the site you gave me
Telegram Sam
16 years ago
4 months ago
5,082
Premium
Phoenix Arrow
16 years ago
2 years ago
825
Can I make sarcastic comments every time Jaygull or someone makes a long post about some obscure politics thing now then? In the interests of equality.
Telegram Sam
16 years ago
4 months ago
5,082
Premium
By Phoenix Arrow | Permalink | On 25 October 2012 - 23:52 PM
Can I make sarcastic comments every time Jaygull or someone makes a long post about some obscure politics thing now then? In the interests of equality.

Go right ahead.
Deano
18 years ago
1 day ago
1,380
Ah fuck man, that brings back bad memories of college.
chokosc
15 years ago
9 months ago
1,041
By Deano | Permalink | On 26 October 2012 - 12:24 PM
Ah fuck man, that brings back bad memories of college.


Sad to hear that! Have the next 3 years to study this
Phoenix Arrow
16 years ago
2 years ago
825
Yeah. Programing is hard by the way. Even harder when you're doing it for video games due to physics and stuff. If you go into it thinking "hey, I could make video games" then you're doing it wrong. Having said that, if you genuinely enjoy coding, troubleshooting and working with algorithms then you'll love it. When I was doing my degree, most of the people on the course dropped out. But the people who stayed loved what they were doing more than I've ever seen anyone love what they do.

It is fun though, it's just not a doss subject.
chokosc
15 years ago
9 months ago
1,041
We aren't going deep into it. It is a third of the class I am taking. The other two are electronics and bio medicine. The whole point is to get as ready for a project that we will be starting half way through next year.

Someone on the website you showed me wrote down a working code. It is full of things that I don't know yet so I'll just ask my teacher what to do. Maybe he'll tell me it is enough (what I have already done) seeing as how we haven't really learnt much.
Slashman X
18 years ago
1 year ago
6,000
By Phoenix Arrow | Permalink | On 26 October 2012 - 20:27 PM
Yeah. Programing is hard by the way. Even harder when you're doing it for video games due to physics and stuff. If you go into it thinking "hey, I could make video games" then you're doing it wrong. Having said that, if you genuinely enjoy coding, troubleshooting and working with algorithms then you'll love it. When I was doing my degree, most of the people on the course dropped out. But the people who stayed loved what they were doing more than I've ever seen anyone love what they do.

It is fun though, it's just not a doss subject.


Programming is class

You'll need to Login to comment