Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace IDP_string
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         // Problema 1. Sa se numere cuvintele dintr-o fraza
  21.         private void button1_Click(object sender, EventArgs e)
  22.         {
  23.             string fraza = textBox1.Text;
  24.             int nr_cuv = 0;
  25.  
  26.             // Inlocuiesc toate spatiile duble, triple, ... cu un singur spatiu
  27.             while (fraza.IndexOf("  ") >= 0)
  28.                 fraza = fraza.Replace("  ", " ");
  29.  
  30.             // Elimin spatiile libere de la inceput si sfarsit de fraza
  31.             fraza = fraza.TrimStart();
  32.             fraza = fraza.TrimEnd();
  33.  
  34.             // Numarul de cuvinte va fi numarul de spatii + 1
  35.             nr_cuv = (fraza.Count(v => (v == ' ')) + 1);
  36.             textBox6.Text = nr_cuv.ToString();
  37.         }
  38.  
  39.         // Problema 2. Sa se identifice secventa numerica din string cea mai mare
  40.         private void button2_Click(object sender, EventArgs e)
  41.         {
  42.             string cuvant = "ab12cd1234e1";
  43.             int nr = 0;
  44.             for (int i = 0; i < cuvant.Length; i++)
  45.                 for (int j = cuvant.Length - 1;  j > i; j--)
  46.                     try
  47.                     {
  48.                         int n_curent = int.Parse(cuvant.Substring(i, j - i + 1));
  49.                         nr = (n_curent > nr) ? n_curent : nr;
  50.                     }
  51.                     catch (Exception ex)
  52.                     {
  53.                         textBox4.AppendText(cuvant.Substring(i, j - i + 1) + "\r\n");
  54.                     }
  55.             textBox6.Text = nr.ToString();
  56.         }
  57.  
  58.         // Problema 3 - Sa se inlocuiasca toate aparitiile stringului "idp"cu "laborator IDP"
  59.         private void button3_Click(object sender, EventArgs e)
  60.         {
  61.             string initial = textBox1.Text;
  62.             textBox6.Text = initial.Replace("idp", "laborator IDP");
  63.         }
  64.  
  65.         // Se considera un string. Se cere sa se afiseze daca acesta este palindrom
  66.         // fara a fi case-sensitive (nu se face diferenta intre majuscule si minuscule)
  67.         // Ex. ana
  68.         private void button4_Click(object sender, EventArgs e)
  69.         {
  70.             // Se converteste valoarea de intrare in litere mici
  71.             string s = textBox1.Text.ToLower();
  72.  
  73.             // Se verifica daca valoarea de intrare este palindrom
  74.             if (s.Equals(String.Join("", s.Reverse())))
  75.                 textBox6.Text = "Palindrom";
  76.             else
  77.                 textBox6.Text = "Nu este palindrom";
  78.         }
  79.  
  80.         // Se considera un sir sw caractere. Se cere sa se numere vocalele
  81.         private void button5_Click(object sender, EventArgs e)
  82.         {
  83.             string s = textBox1.Text;
  84.             string vocale = "aeiou";
  85.             int nr = 0;
  86.             for (int i = 0; i < s.Length; i++)
  87.                 if (vocale.Contains(s[i]))
  88.                     nr++;
  89.             textBox6.Text = nr.ToString();
  90.         }
  91.  
  92.         // Sa se insereze intre fiecare doua caractere ale unui string semnul !
  93.         private void button6_Click(object sender, EventArgs e)
  94.         {
  95.             string s = "oaie";
  96.             for (int i= 0; i < s.Length; i = i + 2)
  97.                 s = s.Insert(i, "!");
  98.             textBox6.Text = s;
  99.         }
  100.  
  101.         // Sa se verifice daca un string poate fi convertit intr-un numar zecimal cu doua zecimale
  102.         private void button7_Click(object sender, EventArgs e)
  103.         {
  104.             string s = textBox1.Text;
  105.             double nr = 0;
  106.             try
  107.             {
  108.                 if (s.Contains(".") && ((s.Length - s.IndexOf(".")) == 3))
  109.                     nr = Convert.ToDouble(s);
  110.             }
  111.             catch (Exception ex)
  112.             {
  113.                 textBox6.Text = "Nu se poate converti";
  114.             }
  115.             textBox6.Text = nr.ToString();
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement