Programming Help

soulslicer

Tank
Joined
Mar 16, 2007
Messages
4,623
Reaction score
12
Okay, I have this test paper with the following questions I can't seem to answer:

1. Write a Console Application that allows a user's input such as "This is a line" to have characters to be reversed in order.

2. Write a Console Application that replaces all occurences of the string no to yes

3. Write an Application that places double quotes on each word in a string

any help?
 
Did you start at all or are you asking for someone to do your homework for you?

It would be helpful to start on the application yourself then post here where you are having issues.
 
#2 should be easy. Come on, are you even paying attention?

If String1="no" or "NO" or "No" then String1="Yes"


just follow the syntax of whatever language you are using, of course
 
Okay, I have this test paper with the following questions I can't seem to answer:

1. Write a Console Application that allows a user's input such as "This is a line" to have characters to be reversed in order.

2. Write a Console Application that replaces all occurences of the string no to yes

3. Write an Application that places double quotes on each word in a string

any help?

What language are you using? What specific issues are you running in to?

Don't expect people to do your homework for you, but I'm sure we'd be happy to help if you had any specific problems.

The language you're using is very important in answering this question. For instance, if you were writing this in perl, 1 2 and 3 would be entirely trivial. If you were writing this in Java, they would not be nearly as trivial. If you were writing this in SML, it may be impossible.
 
Assuming you're still using C#:

1:

Code:
var bla = (from c in "sdfsdf"
                 select c).Reverse();

This gives back a sequence of characters, but reversed. If you wanna turn it into a string then, well, do some research, not gonna do all work for you :p

2:

What you want is someString.Replace, the important thing to note here is that string are immutable, so Replace doesn't change the string you invoke it on, but returns a new string.

3:

Easiest here is to split the string, then join it again with extra quotes. Bold added as a hint :p

On second thought, the last one doesn't work perfectly, but should take you 90% of the way.

There's actually a better way, now that I think of it. It also utilizes Replace. But again, it's not perfect because it doesn't quote ALL words with double quotes. That can be fixed too, though.
 
Okay, sorry I forgot to post my answers/attempts..

Okay for No 2.

I tried:
i figured it
Code:
string power = "This is a line with many yes and yes and no";
            string q = "yes";
            string w = "no";
            power = power.Replace(q,w);
            Console.WriteLine(power);
            Console.ReadLine();
.

For No.1

I absolutely have no idea how to do it. I thought of doing this..
Code:
string m = "power point";
            char[] contain = m.ToCharArray();
            int g = contain.Length;
            do
            {
                Console.Write(contain[g-1]);
                g--;
            }
            while (g > 0);
            Console.ReadLine();

this will work, but is a completely stupid method of getting it done. took like 1s just to calculate that. im sure there is a better way.

As for No 3.

i tried it similar to No. 1, to use replace
but that would not work AS...
Code:
string yug = "This is a line";
            string old= " ";
            string neww = " " " ";

look at the last string, how can u have a string store inverted commas as a well..string.. :angry:

helplife2.net pls help
 
Reversing a string:

something kinda like this (untested)

Code:
string subtitle = "test";
string reverse = string.empty;

  for (int i = subtitle.Length; i > 0; i--)
            reverse += subtitle[i];

Basically starts at end of the string, and copies each character from end to beginning into a new string.

for 3 here's a hint: What separates words in a language? Blank spaces, right? So to figure out where to place your double-quotes, you have to figure out the beginning and end of each word and place them there.

You can use the string methods to do this, like IndexOf and Substring. You could also convert the string into an array of characters and just use the "charAt" method to see if the current character is a blank space, and then just insert a double quote followed by a blank space there (to preserve separation between words).
 
from my programing days about ten or twenty years ago you use double quotes "" I think. What if you wanted double quotes in your string? Then use four I guess. (plus the two to wrap the string)

or whatever. Inverted comma? Sorry. But it should work on the same principle.

EDIT: holy ****ing shit how did I even remember that.
 
Okay, sorry I forgot to post my answers/attempts..

Okay for No 2.

I tried:
i figured it
Code:
string power = "This is a line with many yes and yes and no";
            string q = "yes";
            string w = "no";
            power = power.Replace(q,w);
            Console.WriteLine(power);
            Console.ReadLine();
.

For No.1

I absolutely have no idea how to do it. I thought of doing this..
Code:
string m = "power point";
            char[] contain = m.ToCharArray();
            int g = contain.Length;
            do
            {
                Console.Write(contain[g-1]);
                g--;
            }
            while (g > 0);
            Console.ReadLine();

this will work, but is a completely stupid method of getting it done. took like 1s just to calculate that. im sure there is a better way.

As for No 3.

i tried it similar to No. 1, to use replace
but that would not work AS...
Code:
string yug = "This is a line";
            string old= " ";
            string neww = " " " ";

look at the last string, how can u have a string store inverted commas as a well..string.. :angry:

helplife2.net pls help

To get (") in your string, use an escape charachter, ie

Code:
string neww = " \""


this would end up replacing every space with the string (_") where _ is a space.

Then you just insert (") at the beginning of the string and at the end.

EDIT: I don't know what you're doing with number 2. Are you trying to echo back to the user whatever they typed in, but backwards?

If that's the case, set a string equal to Console.Readline() then output that string's reverse using C#'s reverse method. If you're not allowed to use this method, make your own reverse method.
 
Back
Top