top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C#: c# code to import text from text file and analyze the selected character?

+1 vote
583 views

Develop a C# windows form application that allows users to do all of the following:
Import a text from a text file (*.txt).The input file is selected from an open file dialog.
Successfully imported text is displayed on a text box.
Analyze a selected character.

For example the selected char is # :
is Digit :False
is letter :False
is letter or Digit :False
is lower :False
is upper :False
is punctuation :True
is symbol :False

then :
Calculate number of lines in the imported text.
Calculate number of words in the imported text.
Calculate number of letters in the imported text.
Calculate number of digits in the imported text.
Calculate number of uppers in the imported text.
Replace a selected word by anything else.

posted Mar 8, 2015 by As M Ob

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

What you need something like (you need to work at your end to make it work I am just providing bit and pieces -

        StreamReader reader;
        reader = new StreamReader(@"C:\Users\Documents\data.txt");
        do
        {
            ch = (char)reader.Read();
            /* ... 
                Now you got the char and task is to find out following -
                is digit - use Char.IsDigit(ch) 
                is Letter - use Char.IsLetter(ch)
                is Punctuation - use Char.IsPunctuation(ch) 
                is Symbol - use Char.IsSymbol(ch)  
             */
        } while (!reader.EndOfStream);

Now rest is your homework to complete the program from the given hints :)

answer Mar 9, 2015 by Salil Agrawal
what the data type of ch !
var ch = textBox1.SelectedText;
            MessageBox.Show("is Digit" + char.IsDigit(ch))
why this statment give me error - i want to select char from textbox and then analyze it ! how can doing this !help me
Similar Questions
+2 votes

Develop a C# Windows Form Application that allows users to do all of the following.
Read a List of patient's information from a text file (*.txt), the input text file is selected from the Open File Dialog.
Your program should accept any text file with the following format:
a. The file contains columns with Basic information about patients.
b. Columns may be separated by spaces and/or tabs.
c. The first line in the file is the column header.
d. After the header, each line represents a Patient (name, address, phone#, Bdate, gander ,wheight, height and blood type).
e. Successfully imported patients are displayed on a data grid view in a second form and added to a patient list.

  1. The user will be able to display on The Grid view :
    a) Female patients.
    b) Patients with age<45 year. c) Save Over weighted patients on a text file .  Note: To find over weighted patients you have to calculate the BMI value. BMI=Weight/ (Height*Height). If BMI <18.5 >>>>> under weighted.
    18.5<=BMI<=25 >>>>>>>Normal.
    BMI>25 >>>>>>>>>>>> Over Weighted.
+2 votes

if form1 include textbox - then selected word from it and
go to form2 to input new word and replace it with selected word
then display form1 with textbox that include the new word

+1 vote

If write this code in form 1 :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml.Serialization;

namespace PostLab5_**********
{
    public partial class Form1 : Form
    {
        List<StudentInfo> Stu_Info = new List<StudentInfo>();
        Queue<StudentInfo> pass = new Queue<StudentInfo>();
        Queue<StudentInfo> fail = new Queue<StudentInfo>();
        Stack<StudentInfo> Excelent = new Stack<StudentInfo>();
        Stack<StudentInfo> VeryGood = new Stack<StudentInfo>();
        Stack<StudentInfo> Good = new Stack<StudentInfo>();
        public Form1()
        {
            InitializeComponent();
        }

        private void importXMLToolStripMenuItem_Click(object sender, EventArgs e)
        {

            OpenFileDialog Dlg = new OpenFileDialog();
            Dlg.Filter = "XML File|*.xml";
            if (Dlg.ShowDialog() == DialogResult.OK)
            {
                StreamReader Infile = new StreamReader(Dlg.FileName);
                XmlSerializer Des = new XmlSerializer(typeof(List<StudentInfo>));
                Stu_Info = (List<StudentInfo>)Des.Deserialize(Infile);
                Infile.Close();
                dataGridView1.DataSource = Stu_Info;
            }
        }

        private void passedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var R = from Item in Stu_Info
                    where Item.total >= 50
                    select Item;
            dataGridView1.DataSource = R.ToArray();
            foreach (StudentInfo item in R)
            {
                pass.Enqueue(item);
            }
        }

        private void failedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var R = from Item in Stu_Info
                    where Item.total < 50
                    select Item;
            dataGridView1.DataSource = R.ToArray();
            foreach (StudentInfo item in R)
            {
                fail.Enqueue(item);
            }
        }

        private void excellentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var R = from Item in Stu_Info
                    where Item.total >= 84
                    select Item;
            dataGridView1.DataSource = R.ToArray();
            foreach (StudentInfo Item in R)
            {
                Excelent.Push(Item);
            }
        }

        private void veryGoodToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var R = from Item in Stu_Info
                        where Item.total >=76 && Item.total <84
                        select Item;
                dataGridView1.DataSource = R.ToArray();
                foreach (StudentInfo Item in R)
                {
                    VeryGood.Push(Item);
                }
            }

        private void goodToolStripMenuItem_Click(object sender, EventArgs e)
        {
             var R = from Item in Stu_Info
                        where Item.total >= 68 && Item.total < 76
                        select Item;
                dataGridView1.DataSource = R.ToArray();
                foreach (StudentInfo Item in R)
                {
                    Good.Push(Item);
                }
        }

        private void countOfVeryGoodToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(VeryGood.Count().ToString());
        }

        private void peekOfPeekToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Good.Peek().name);
        }

        private void removeItemFromFailedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(fail.Dequeue().name);
        }

        private void countOfPassedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(pass.Count().ToString());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();

        }
        }
    }

and in form2 contain textboxes to add new item for these list ! how use this !

...