Jump to content

Need help with a C# program

- - - - -

  • Please log in to reply
5 replies to this topic

#1
ingoz

ingoz

    Newbie

  • Members
  • Pip
  • 3 posts
Basically I am to create a program that reads a written string and identifies where two letters come together and puts stars("*") before and after every pair of letters. "Complete package" would stay the same as "Messaging your buddy" would become "Me*ss*aging your bu*dd*y". I know this is how I'm supposed to start the program but I have no idea how to keep working with it.
For clarification "Sláðu inn orð:" means "Type a word", "uttak" means something like an outtake, basically what the program writes out and "par" means "pair". I'm doing this in icelandic, not really sure how I should translate all the words :< . If anyone could help me with atleast the next step to take or any steps I'm missing I'd be very grateful .

using System;

class T203Fv6_6 {

static void Main() {



Console.Write("Sláðu inn orð: ");

string ord = Console.ReadLine();
String uttak=String.Empty;
bool par=false;

}
}

#2
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
Since this is an assignment for your class I really can't give you the easy solution to your problem. I can give you some steps you need to follow, though.
  • You need something to hold the new string as you build it. StringBuilder is ideal for this.
  • Iterate through the source string character by character.
    • Are we past the last character (possible if the last two characters are the same)
    • If Yes -> exit this loop
    • Are we on the last character?
    • If Yes -> add the current character to our new string and exit this loop.
    • Check if the current character is the same as the one after it.
    • If Yes -> add '*', the current character, the next character, and '*' to our new string. Increase the 'pointer' that references the current character by 2.
    • If No -> add the current character to our new string. Increase the 'pointer' that references the current character by 1.
  • The new string now has what you need


Just FYI, all this can be done with one line. You probably haven't learned how yet, so don't sweat over it trying to solve it that way

#3
ingoz

ingoz

    Newbie

  • Members
  • Pip
  • 3 posts
Hey, thanks alot for your response, problem is that from all I've learned in programming I have no idea how to do those steps that you just described. I'm aware what needs to be done I just have no idea how to do it XD.

#4
Aelustelin

Aelustelin

    Newbie

  • Members
  • Pip
  • 2 posts
This program will be easy to make if you set up a second string to recieve the old string *letter by letter* through the use of if statements. What I mean is, become familiar with the concept of a "substring". Then use that to take the old string character by character and check to see if there are matching characters. If not, throw the non matching characters into the new string, if so, put a star in before, then after the pair.

pseudocode to help you:
if(the substring comes up with a pair)
{
add * to newstring;
add the matching characters;
add another *
}
else
{
add the nonmatching characters to newstring;
}

I hope that makes sense. I am trying to help you out as much as I can without ruining the point of the assignment.

#5
ingoz

ingoz

    Newbie

  • Members
  • Pip
  • 3 posts
Thank you so much for the response but I must admit I'm completely lost. My textbook for the course is vague at best, nowhere in it is even a mention on substrings or even as much as a link to a website with details on the subject. I've tried to use both google and MSDN but I still cant find anything to help me, the project is due in less then 4 hours and I'm completely clueless. Yes, I do realize I'm terrible at programming, hence the classes :P. I manage to find out the command for creating the substring and defining it but I have no idea how to search for pair of words. :<

using System;

class T203Fv6_6 {

static void Main() {

Console.Write("Sláðu inn orð: ");

string ord = Console.ReadLine();
string sub = ord.Substring(0, ord.Length);
//String uttak=String.Empty;
bool par=false;

}
}

#6
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
In your code above, you took a substring of the entire string, which doesn't help you at all.

You can take a substring of two characters like so:

String sub = ord.Substring(x, 2);

or you can take a single character like so:

String sub = ord.Substring(x, 1);

Notice I put x for the starting position. That's a hint. You're going to need to declare a variable and use it to "step" through the string letter by letter, and this variable will keep track of where you are so you know from which offset to begin taking the substring.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users