Find Substring and Reverse it in Windows Store App

Introduction

In this article I will explain how to find a substring from a string and if present then reverse the substring. Basically it is a frequently asked interview question. I have encountered this question many times in interviews. That is why I decided to write this article.

Now use the following procedure to implement it.

Step 1

First of all you have to create a new Windows Store Application, as in:

  • Open Visual Studio 2012
  • "File" -> "New" -> "Project..."
  • Choose "Template" -> "Visual C#" -> "Window Store app"
  • Choose "Blank App (XAML)" then rename the application

new-windows-store-app.jpg

Step 2

Write the following XAML code in "Mainpage.Xaml" (that is available in Solution Explorer):

<Page

    x:Class="number_app.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:number_app"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

 

    <Grid Background="Red">

        <Grid.RowDefinitions>

            <RowDefinition Height="116*"/>

            <RowDefinition Height="60*"/>

            <RowDefinition Height="52*"/>

            <RowDefinition Height="48*"/>

            <RowDefinition Height="40*"/>

            <RowDefinition Height="53*"/>

            <RowDefinition Height="310*"/>

            <RowDefinition Height="89*"/>

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="328*"/>

            <ColumnDefinition Width="353*"/>           

            <ColumnDefinition Width="685*"/>

        </Grid.ColumnDefinitions>

        <TextBlock Text="Enter string :" FontSize="20" FontWeight="ExtraBold" FontFamily="Arial" Grid.Column="1" Grid.Row="2" ></TextBlock>

        <TextBox x:Name="textbox1" Grid.Column="2" Grid.Row="2" Width="200" Height="32" HorizontalAlignment="Left" VerticalAlignment="Top"></TextBox>

        <TextBlock Text="Enter string :" FontSize="20" FontWeight="ExtraBold" FontFamily="Arial" Grid.Column="1" Grid.Row="2" ></TextBlock>

        <TextBlock Text="Enter substring for Reverse :" FontSize="20" FontWeight="ExtraBold" FontFamily="Arial" Grid.Column="1" Grid.Row="3" ></TextBlock>

        <TextBox x:Name="textbox2" Grid.Column="2" Grid.Row="3" Width="200" Height="32" HorizontalAlignment="Left" VerticalAlignment="Top"></TextBox>

        <Button x:Name="button1" Content="click" Grid.Column="2" Grid.Row="4" Width="104" Click="button1_Click" Background="Yellow" Foreground="Black" Height="38" Margin="0,3,0,0" Grid.RowSpan="2" VerticalAlignment="Top" />

        <TextBlock x:Name="text1" FontSize="20" FontWeight="ExtraBold" FontFamily="Arial" Grid.Column="2" Grid.Row="5" Width="619" HorizontalAlignment="Left"  ></TextBlock>

    </Grid>

</Page>

 

Step 3

Now write the following C# code for the button within "Mainpage.Xaml.cs".

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;

using System.Text;

 

namespace number_app

{

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

        }      

        protected override void OnNavigatedTo(NavigationEventArgs e)

        {          

        }          

        public void number(string str)

        {              

           bool b= str.Contains(substr);

           string temp = "";

           if (b == true)

           {

               for (int i = substr.Length - 1; i >= 0; i--)

               {

                   temp += substr[i];

               }

               str = str.Replace(substr, temp);

               text1.Text = str;

           }

           else

           {

               text1.Text = "substring " + substr + " not found";

           }

        }

        string str;

        string substr;

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            str = null;

            substr = null;

            str = textbox1.Text;

            substr = textbox2.Text;

            number(str);           

        }

    }

}

 

Step 4

Now Run your app.

run-string-app.jpg

substring-not-found.jpg

output-of-string-app.jpg


Similar Articles