Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Okay guys, i'm sure you don't like people that submit their homework here, but i only come here as a last resort because i really need help.

Fyi; I'm a beginner on programming.

The task is:





Examples of program execution

Enter a word: Bob
* b * o * b *
bob encrypted becomes CPC

What I have tried:

I've gotten this far, but i can't seem to get the Asterisk (*) to get between the written word, and also i can't seem to get the encryption right e.g A > B

C#
using System;
using System.Text;
using System.Collections;

namespace Methods
{
    class Program
    {
        static void Main(string[] args)
        {
            char temp;
            string myStr;
            Console.Write("Enter a word or a city to see magic: ");
            myStr = Convert.ToString(Console.ReadLine());
            Magic("");

            string str = myStr.ToLower();
            char[] charstr = str.ToCharArray();

            for (int i = 1; i < charstr.Length; i++)
                {
                  for (int j = 0; j < charstr.Length - 1; j++)
                  {
                     if (charstr[j]>charstr[j+1])
                        { temp = charstr[j];
                          charstr[j] = charstr[j + 1];
                          charstr[j + 1] = temp;
                        }
                  }
                  Console.WriteLine(charstr);
                  Console.ReadLine();
               }

        }

        public static void Magic(string heading, char tecken = '*')
        {
            int antal = heading.Length;
            for(int i = 0; i < antal + 6; i++) {
               Console.Write(tecken + heading + tecken); }
            }
        }
    }
}
Posted
Updated 2-Dec-19 5:50am
v6

There are easy ways of doing this if you are familiar with linq and extension methods. Something like the following, encapsulated within methods.

C#
//surround hello with *
string input = "hello";
var starsInBetween= string.Join("*", input.ToCharArray());
string surroundedByStars= $"*{starsInBetween}*";

//shift char value by +1
var charArray = "Bob".ToCharArray();
var encryptedArray = charArray.Select(a => ++a).ToArray();
var encryptedString = new string(encryptedArray);

 
Share this answer
 
Comments
bouya98 29-Nov-19 8:31am    
But that is only affecting "Hello" and "bob" right?, i want it to affect the input of the user. This task is so hard
George Swan 29-Nov-19 9:42am    
Try looking up 'C# Console class' to learn about inputting and outputting text. Then look up 'C# Methods' to learn how they are used. If I simply post the full solution on here you will be none the wiser and your tutor will smell a rat.
bouya98 29-Nov-19 13:01pm    
not really, our teacher has taught us barely nothing and asks us to do a task like this. We barely know how to do a foor loop
George Swan 29-Nov-19 15:40pm    
Sorry to hear that. Try searching for 'teach yourself c#' and take it from there.
Magic is written incorrectly... You have to create new string inside a loop to display it finally.
1) in the first step -> newstring += "* " + char[i] + " * "
2) in every middle step -> newstring += char[i] + " * "
3) in the last step -> newstring += char[i] + " *"

I'd suggest to use StringBuilder[^], because string is immutable[^].

I do not see second method...
It's quite simple. You need to iterate through the chars of string and add 1 to the ascii value of each char.

C#
char s = 'S';
char t = (char)((int)s + 1);
//t = "T" ;)

Do not forget to use StringBuilder!

Good luck!
 
Share this answer
 
Comments
bouya98 28-Nov-19 13:51pm    
I don't know what stringbuilder is :o
Maciej Los 28-Nov-19 14:05pm    
So, follow the link i've provided.
bouya98 28-Nov-19 14:22pm    
Should i make a stringbuilder in main or in the methods?
Maciej Los 28-Nov-19 14:46pm    
Inside the methods.
Here you go:
static void Main(string[] args)
{
    Console.Write("Enter a word or a city to see magic: ");
    string myStr = Console.ReadLine();
    Magic1(myStr);
    Console.WriteLine(Magic2(myStr));
    Console.ReadKey();
}

public static void Magic1(string heading)
{
    string result = string.Empty;

    foreach (char c in heading)
    {
        char c2 = (char)(c + 1);
        result += c2.ToString();
    }

    Console.WriteLine(result);
}

public static string Magic2(string heading, string tecken = "*")
{
    string result = tecken;

    foreach (char c in heading)
    {
        result += c.ToString() + tecken;
    }

    return result;
}


Here is an example for StringBuilder:
https://www.dotnetperls.com/stringbuilder[^]
For this code it is of little use, only when processing longer strings it can speed up things.
 
Share this answer
 
v4
Comments
bouya98 28-Nov-19 13:26pm    
Ricky! Wow, it seems to work when i put it like this Magic1("Stockholm ");
Magic2("Stockholm ");
But how do i make it so it changes the input from the user to Magic1 and 2?

So like if the user types, Stockholm it comes out - * S * T * O * C * C * H * O * L * M *
STOCKHOLM encrypted becomes TUPDLIPMN
Maciej Los 28-Nov-19 13:41pm    
AS per requirements, the second method should return string. So, it can't be a void method.
bouya98 28-Nov-19 13:43pm    
Help me please Sir, i have no idea how to do that :/, just keep getting error " Severity Code Description Project File Line Suppression State Suppression State
Error CS0161 'Program.Magic2(string, string)': not all code paths return a value"
When trying to change to string from void
RickZeeland 28-Nov-19 13:44pm    
Got me there again :)
bouya98 6-Dec-19 20:47pm    
Ricky your answer worked splendid btw. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900