C# Lesson 9

Lesson 9 in our series about learning C# teaches you how to make Strings do your bidding.

In this lesson we are going to learn how to iterate over a string and ignore case when comparing them.
Afterwards you can continue to the next lesson!
Or go back and review the previous lesson.
We also have the source code on GitHub.


using System;

namespace Lesson9
{
    class StringManipulation
    {
        static void Main(string[] args)
        {
            string alphabet = "abcd";
            for (int index = 0; index < alphabet.Length; index++)
            {
                //string letter = alphabet[index]; // Doesn't work
                char letter = alphabet[index];
                Console.WriteLine($"char at index {index}: {letter}");
            }

            //Index out of bounds rant
            string reverse = "";
            for (int index = alphabet.Length - 1; index >= 0; index--)
            {
                //string letter = alphabet[index]; // Doesn't work
                char letter = alphabet[index];
                Console.WriteLine($"char at index {index}: {letter}");
                reverse += letter;
            }

            Console.WriteLine(reverse);


            // using ToLower and ToUpper for string compaire
            string userInput;

            do
            {
                Console.Write("Type 'exit' to quit: ");
                userInput = Console.ReadLine();

            } while (userInput.ToUpper() != "exit");


        }
    }
}


/*
* TASK:
*   Make a Program that takes a string from the user the displays if the string is a palindrom or not.
*   A palindrome is a word, phrase, number or sequence of words that reads the same backward as forward.
*   Spaces between words is allowed and must be dealt with by the program. 
*   
*   Assume any other punctuation will not be entered by the user. Example: ',".? ect... 
*   
*   Example Output:
*       Enter a string:
*       Level
*       level is a palindrome!
*   
*   
*   Example Output:
*       Enter a string:
*       Blue
*       blue is not a palindrome!
*   
*   
*   Example Output:
*       Enter a string:
*       My GYm
*       my gym is a palindrome!
*   
*   
*   
*   NOTE: You will have to handle both uppercase and lowercase letters.
*/

using System;

namespace Lesson9Task
{
    class StringManipulationTask
    {
        static void Main(string[] args)
        {
        }
    }
}

Recommended posts

We have similar articles. Keep reading!

C# Lesson 1

Learn how to declare and use variables in the very first lesson from our series on learning C# for beginners.

C# Lesson 5

Run sections of your code only IF you want them to with the "if" statement in the 5th lesson from the series on learning C# for beginners.

C# Lesson 8

Characters, the data type that's just a little less useful than a string.

C# Lesson 7

You're familiar with "while" loops in C#, but but it's time to learn about "for" loops. Let's get loopy in the 7th lesson in our series about learning C#.

Comments

Log in or sign up to leave a comment.