कंप्यूटर प्रोग्रामिंग/स्ट्रिंग्स/सी शार्प

strings.cs सम्पादन

// This program splits a given comma-separated name into first and last name
// components and then displays the name.

using System;

public class Strings
{
    public static void Main(String[] args)
    {
        string name;
        string first;
        string last;
        
        name = GetName();
        last = GetLast(name);
        first = GetFirst(name);
        DisplayName(first, last);
    }

    private static string GetName()
    {
        string name;
        int index;
    
        do
        {
            Console.WriteLine("Enter name (last, first):");
            name = Console.ReadLine();
            index = name.IndexOf(",");
        } while (index < 0);
        
        return name;
    }

    private static string GetLast(string name)
    {
        string last;
        int index;
    
        index = name.IndexOf(",");
        if(index < 0)
        {
            last = "";
        }
        else
        {
            last = name.Substring(0, index);
        }
        
        return last;
    }

    private static string GetFirst(string name)
    {
        string first;
        int index;
    
        index = name.IndexOf(",");
        if(index < 0)
        {
            first = "";
        }
        else
        {
            first = name.Substring(index + 1, name.Length - index - 1);
            first = first.Trim();
        }
    
        return first;    
    }

    private static void DisplayName(string first, string last)
    {
        Console.WriteLine("Hello " + first + " " + last + "!");
    }
}

कोशिश करो सम्पादन

निम्न कोड मुफ्त ऑनलाइन विकास के वातावरण में से एक में ऊपर कॉपी और पेस्ट करो या अपने खुद के कम्पाइलर/इंटरप्रेटर/आईडीई का उपयोग करें।

यह भी देखें सम्पादन