-4

I have problem with my code. I don't know what am I doing wrong. I want for user to write some sentence and then program should split written sentence into single words where I use ToUpper on first chars of each word in array and then join the together again without spaces.

my code is:

    static void Skrci(string stavek)
    {
        string[] p;
        p = stavek.Split(' ');  // polje separatov

        for (int i = 0; i < p.Length; i++)
        {
            if (p[i].Length > 0)
            {
                char zacetnica = Char.ToUpper(p[i][0]);
                p[i] = p[i].Remove(0, 1);
                p[i] = p[i].Insert(0, zacetnica.ToString());
            }
        }
        stavek = string.Join(" ", p);
    }

    static void Main(string[] args)
    {
        string[] p = null;
        Console.Write("Vpiši nek stavek: ");
        string stavek = Console.ReadLine();
        Skrci(stavek);
        Console.WriteLine(stavek);
        Console.ReadKey(true);
    }

I tried Step into to see if I can see what is wrong but I don't know.

Amar Palsapure
  • 9,590
  • 1
  • 27
  • 46
  • 4
    So give an example on how the program goes wrong. We dont really do your homework here. – TomTom Jan 12 '12 at 08:21
  • What is the error you're receiving? – user978122 Jan 12 '12 at 08:21
  • 3
    Try to use english in your code. It will help people here to understand it better. – Bas Slagter Jan 12 '12 at 08:21
  • My native language is Slovenian and its hard to write what I need in English. – Kristy Maitz Jan 12 '12 at 08:42
  • 1
    @KristyMaitz Your reputation fell because your question was downvoted. One downvote subtracts two points from your reputation, as explained in the FAQ (http://stackoverflow.com/faq#reputation). Downvotes can be a result of people finding your question unclear, poor, or that you perhaps should have researched it more yourself before askig for help. I can recommend to you Jon Skeet's "recipe" for writing the perfect question: https://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx – Julian Jan 12 '12 at 08:54

5 Answers5

8

Your problem is that your function Skrci does not return a value (or, update it's parameter (by reference))

Here's a quick fix

static string Skrci(string stavek)
{
    string[] p;
    p = stavek.Split(' ');  // polje separatov

    for (int i = 0; i < p.Length; i++)
    {
        if (p[i].Length > 0)
        {
            char zacetnica = Char.ToUpper(p[i][0]);
            p[i] = p[i].Remove(0, 1);
            p[i] = p[i].Insert(0, zacetnica.ToString());
        }
    }
    return string.Join(" ", p);
}

static void Main(string[] args)
{
    string[] p = null;
    Console.Write("Vpiši nek stavek: ");
    string stavek = Console.ReadLine();
    stavek = Skrci(stavek);
    Console.WriteLine(stavek);
    Console.ReadKey(true);
}
Shai
  • 25,159
  • 9
  • 44
  • 67
5

One easy way to convert your string into Camel Case, is to create yourself the following extension method:

public static String ToCamelCase(this String source)
{
    return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(source.ToLower());
}

(credit to https://stackoverflow.com/a/1168346/310001)

You can then simply do like this:

string stavek = Console.ReadLine().ToCamelCase();

Edit:
And if you want to remove the spaces between the words, you can of course simply add .Replace(" ", "") at the end.

Community
  • 1
  • 1
Julian
  • 20,008
  • 17
  • 77
  • 108
  • I never used ToCamel.Case - its one method professor never mentioned. – Kristy Maitz Jan 12 '12 at 08:44
  • @KristyMaitz `ToCamelCase` is a method I've written myself (as shown in the answer), it doesn't exist in the C# language... You can of course write `string stavek = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Console.ReadLine().ToLower())` directly, but it's much nicer wrapping it in a function, more clearly explaining its intention. – Julian Jan 12 '12 at 08:49
2

You should pass your string to function as a reference, not as a value. Use ref keyword for that.

Example:

static void Skrci(ref string stavek)
    {
        string[] p;
        p = stavek.Split(' ');  // polje separatov

        for (int i = 0; i < p.Length; i++)
        {
            if (p[i].Length > 0)
            {
                char zacetnica = Char.ToUpper(p[i][0]);
                p[i] = p[i].Remove(0, 1);
                p[i] = p[i].Insert(0, zacetnica.ToString());
            }
        }
        stavek = string.Join(" ", p);
    }

    static void Main(string[] args)
    {
        string[] p = null;
        Console.Write("Vpiši nek stavek: ");
        string stavek = Console.ReadLine();
        Skrci(ref stavek);
        Console.WriteLine(stavek);
        Console.ReadKey(true);
    }
Sergey Kudriavtsev
  • 10,328
  • 4
  • 43
  • 68
  • 1
    `ref` is generally confusing; while it is immensely useful (and I use it lots), for *this* scenario, simply returning the string is far preferable; see Shai's answer for a good example. Also - semantics: the string was **already** being passed "as a reference"; what you mean is pass the string reference *by reference*. – Marc Gravell Jan 12 '12 at 08:23
  • methods are new to me and am learning proper way to use ref, out ect. – Kristy Maitz Jan 12 '12 at 08:45
  • 1
    @KristyMaitz: Thanks for accepting, but Shai's soultion with returning a string from function really sounds better... – Sergey Kudriavtsev Jan 12 '12 at 08:55
0

If I understand you right, this is what you are trying to do:

    static string Skrci(string stavek)
    {
        string[] p;
        p = stavek.Split(' ');  // polje separatov

        for (int i = 0; i < p.Length; i++)
        {
            if (p[i].Length > 0)
            {
                char zacetnica = Char.ToUpper(p[i][0]);
                p[i] = p[i].Remove(0, 1);
                p[i] = p[i].Insert(0, zacetnica.ToString());
            }
        }
        stavek = string.Join(String.Empty, p);

        return stavek;
    }

    static void Main(string[] args)
    {
        string[] p = null;
        Console.Write("Vpiši nek stavek: ");
        string stavek = Console.ReadLine();
        stavek = Skrci(stavek);
        Console.WriteLine(stavek);
        Console.ReadKey(true);
    }

If you want to concatenate the strings without the whitespace then don't user string.Join with " " as separator. Also, you are not printing the result of the method, just your input string.

granaker
  • 1,318
  • 8
  • 13
0
 static string Skrci(string stavek)
    {
        string[] p;
        p = stavek.Split(' ');  // polje separatov

        for (int i = 0; i < p.Length; i++)
        {
            if (p[i].Length > 0)
            {
                char zacetnica = Char.ToUpper(p[i][0]);
                p[i] = p[i].Remove(0, 1);
                p[i] = p[i].Insert(0, zacetnica.ToString());
            }
        }
        stavek = string.Join("", p);

        return stavek;
    }

    static void Main(string[] args)
    {
        string[] p = null;
        Console.Write("Vpiši nek stavek: ");
        string stavek = Console.ReadLine();
        stavek = Skrci(stavek);
        Console.WriteLine(stavek);
        Console.ReadKey(true);
    }
Obi
  • 3,091
  • 4
  • 34
  • 56