Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace IDP_string
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- // Problema 1. Sa se numere cuvintele dintr-o fraza
- private void button1_Click(object sender, EventArgs e)
- {
- string fraza = textBox1.Text;
- int nr_cuv = 0;
- // Inlocuiesc toate spatiile duble, triple, ... cu un singur spatiu
- while (fraza.IndexOf(" ") >= 0)
- fraza = fraza.Replace(" ", " ");
- // Elimin spatiile libere de la inceput si sfarsit de fraza
- fraza = fraza.TrimStart();
- fraza = fraza.TrimEnd();
- // Numarul de cuvinte va fi numarul de spatii + 1
- nr_cuv = (fraza.Count(v => (v == ' ')) + 1);
- textBox6.Text = nr_cuv.ToString();
- }
- // Problema 2. Sa se identifice secventa numerica din string cea mai mare
- private void button2_Click(object sender, EventArgs e)
- {
- string cuvant = "ab12cd1234e1";
- int nr = 0;
- for (int i = 0; i < cuvant.Length; i++)
- for (int j = cuvant.Length - 1; j > i; j--)
- try
- {
- int n_curent = int.Parse(cuvant.Substring(i, j - i + 1));
- nr = (n_curent > nr) ? n_curent : nr;
- }
- catch (Exception ex)
- {
- textBox4.AppendText(cuvant.Substring(i, j - i + 1) + "\r\n");
- }
- textBox6.Text = nr.ToString();
- }
- // Problema 3 - Sa se inlocuiasca toate aparitiile stringului "idp"cu "laborator IDP"
- private void button3_Click(object sender, EventArgs e)
- {
- string initial = textBox1.Text;
- textBox6.Text = initial.Replace("idp", "laborator IDP");
- }
- // Se considera un string. Se cere sa se afiseze daca acesta este palindrom
- // fara a fi case-sensitive (nu se face diferenta intre majuscule si minuscule)
- // Ex. ana
- private void button4_Click(object sender, EventArgs e)
- {
- // Se converteste valoarea de intrare in litere mici
- string s = textBox1.Text.ToLower();
- // Se verifica daca valoarea de intrare este palindrom
- if (s.Equals(String.Join("", s.Reverse())))
- textBox6.Text = "Palindrom";
- else
- textBox6.Text = "Nu este palindrom";
- }
- // Se considera un sir sw caractere. Se cere sa se numere vocalele
- private void button5_Click(object sender, EventArgs e)
- {
- string s = textBox1.Text;
- string vocale = "aeiou";
- int nr = 0;
- for (int i = 0; i < s.Length; i++)
- if (vocale.Contains(s[i]))
- nr++;
- textBox6.Text = nr.ToString();
- }
- // Sa se insereze intre fiecare doua caractere ale unui string semnul !
- private void button6_Click(object sender, EventArgs e)
- {
- string s = "oaie";
- for (int i= 0; i < s.Length; i = i + 2)
- s = s.Insert(i, "!");
- textBox6.Text = s;
- }
- // Sa se verifice daca un string poate fi convertit intr-un numar zecimal cu doua zecimale
- private void button7_Click(object sender, EventArgs e)
- {
- string s = textBox1.Text;
- double nr = 0;
- try
- {
- if (s.Contains(".") && ((s.Length - s.IndexOf(".")) == 3))
- nr = Convert.ToDouble(s);
- }
- catch (Exception ex)
- {
- textBox6.Text = "Nu se poate converti";
- }
- textBox6.Text = nr.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement