BSc IT Sem 5- Advanced Web Programming Practicals...
In this article I have given the solution and step-by-step explanation for practical 1(b) of Advanced Web Programming subject of BSc IT sem-5 (Mumbai University). I have given a step-wise guidance on creating a new project and designing our webpage to writing the code on Visual Studio. I`ll also explain How the program works? so that It`ll be easier for you to understand the program and manipulate it according to yourself.
Question: Create an application to demonstrate string operations.
Firstly, let us understand what the question demands. We have to create an application that demonstrates various string operations. So, what does it mean?
Explanation:
We have to create an application that will be taking a string input from the user and the application will perform various string operations on the inputted string data. For this we need a TextBox where the user can enter data, a Button to submit the data, another Button to reset the values, 10 Labels to show the different outputs performed on the single string data entered by the user.
Lets now dive into the designing and coding process.
Follow these following steps to perform practical 1(b).
Step 1: Create a new project.
- Select language as "C#", select platform as "Windows"(because I`m using windows OS), select project type as "Web".
- Select ASP.NET Web Application (.NET Framework) with C# language as shown below and click "Next".
Step 2: Configure your new project.
Step 3: Create a new ASP.NET Application.
- Select "Empty".
- Click on "Create".
Your project named "pract1b" has been created.
Step 4: Adding Web Form.
Step 5: Designing.
- Drag and drop the requirements discussed above(textbox, buttons, labels) from the "ToolBox" and design them in the "div" section as shown below.
- Single click on "Label".
- The properties of label appears on the right side.
- Delete the text value and keep it blank.
- Click "Enter" on your keyboard.
- Repeat this process for all the Labels and keep their text values blank.
Step 6: Coding.
- Double click on "Result" button.
- A page named "WebForm1.aspx.cs" opens with all the necessary code.
- Type the below code inside "Button1_Click" where the cursor is pointing.
string s = TextBox1.Text;
Label1.Text = "String length:" + s.Length;
Label2.Text = "Substring:" + s.Substring(4,3);
Label3.Text = "Upper String:" + s.ToUpper();
Label4.Text = "Lower String:" + s.ToLower();
string rev = "";
for(int i=s.Length-1;i>=0;i--)
{
rev = rev + s[i];
}
Label5.Text = "Reverse String:" + rev.ToString();
Label6.Text = "Replace 'r' by 's' in String:" + s.Replace('r', 's');
Label7.Text = "Insert 'u' in String:" + s.Insert(3, "u");
Label8.Text = "String Truncate" + s.Trim();
Label9.Text = "Remove String:" + s.Remove(4);
Label10.Text = "Index of String:" + s.IndexOf('r'); - Go back to the design page(WebForm1.aspx).
- Double click on "Reset" button.
- Type the below code inside "Button2_Click".
Label1.Text = "";
Label2.Text = "";
Label3.Text = "";
Label4.Text = "";
Label5.Text = "";
Label6.Text = "";
Label7.Text = "";
Label8.Text = "";
Label9.Text = "";
Label10.Text = "";
- Save both the pages(WebForm1.apsx.cs and WebForm1.aspx).
- To run your project click "f5" button on your keyboard.
Program Explanation:
Now its time to understand the code that we have written inside the Result and Reset buttons. Its important to understand the code rather than just copy and pasting the code from here into your project file. By copy and pasting, your project will work but you will not know how your from works.
Here I have given explanation for each line of code.
Explanation for "Result" Button code:
string s = TextBox1.Text;
This line declares a string variable s. The value that the user will enter in the text box will be assigned to variable s.
Label1.Text = "String length:" + s.Length;
This line will display the length of the data entered by the user with the message "String length:" in label 1.
Label2.Text = "Substring:" + s.Substring(4,3);
This line will select the character present at index number 4 from user entered data and print 3 letters starting from character at index number 4 in label 2.
Label3.Text = "Upper String:" + s.ToUpper();
This line will print the user entered data in uppercase in label 3.
Label4.Text = "Lower String:" + s.ToLower();
This line will print the user entered data in lowercase in label 4.
string rev = "";
for(int i=s.Length-1;i>=0;i--)
{
rev = rev + s[i];
}
Label5.Text = "Reverse String:" + rev.ToString();
This snippet will print the data in reverse format.
The reversed string will be printed in label 5.
Label6.Text = "Replace 'r' by 's' in String:" + s.Replace('r', 's');
This line will replace the letter 'r' with letter 's' if there is 'r' in the data. This will be printed in label 6.
Label7.Text = "Insert 'u' in String:" + s.Insert(3, "u");
This line will insert letter 'u' at index number 3 inside the data and print it in label 7.
Label8.Text = "String Truncate" + s.Trim();
This line will trim the data and print in label 8.
Label9.Text = "Remove String:" + s.Remove(4);
This line will not print the characters starting from index number 4.
It will be printed in label 9.
Label10.Text = "Index of String:" + s.IndexOf('r');
This line will print the index number of letter 'r' and display it in label 10.
Explanation for "Reset" Button code:
Label1.Text = "";
This line will clear the value of label 1.
Label2.Text = "";
This line will clear the value of label 2.
Label3.Text = "";
This line will clear the value of label 3.
Label4.Text = "";
This line will clear the value of label 4.
Label5.Text = "";
This line will clear the value of label 5.
Label6.Text = "";
This line will clear the value of label 6.
Label7.Text = "";
This line will clear the value of label 7.
Label8.Text = "";
This line will clear the value of label 8.
Label9.Text = "";
This line will clear the value of label 9.
Label10.Text = "";
This line will clear the value of label 10.
Comments
Post a Comment
If you have any doubts, Please let me know.