Question

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.

Was it helpful?

Solution

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);
    }

OTHER TIPS

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);
}

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.

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.

 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);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top