Advertisement
AvengersAssemble

StringEx22-01-14

Jan 22nd, 2014
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         public static bool AandB(string st, int start, int finish) //ex21
  11.         {
  12.             string sub = st.Substring(start, finish - start);
  13.             int countA = 0, countB = 0;
  14.             for (int i = 0; i < sub.Length; i++)
  15.                 if (sub[i] == 'a')
  16.                     countA++;
  17.                 else if (sub[i] == 'b')
  18.                     countB++;
  19.             return countA == countB;
  20.         }
  21.         public static string ReplaceWith(string text, string replaced, string with) //ex12
  22.         {
  23.             text = text.Replace(replaced, with);
  24.             return text;
  25.         }
  26.         public static int CountApps(string text, string word) //ex10
  27.         {
  28.             int count = 0;
  29.             for (int i = 0; i < text.Length - word.Length; i++)
  30.             {
  31.                 if (text.IndexOf(word, i) > 0)
  32.                     count++;
  33.             }
  34.             return count;
  35.         }
  36.         public static int StartsWith(string[] a, string s) //ex9
  37.         {
  38.             int count = 0;
  39.             for (int i = 0; i < a.Length; i++)
  40.                 if (a[i].IndexOf(s) == 0)
  41.                     count++;
  42.             return count;
  43.         }
  44.         static void Main(string[] args) //commented out - ex9's main
  45.         {
  46.             int count = 0, kMax = 6;
  47.             string st;
  48.             st = Console.ReadLine();
  49.             for (int i = 0; i < st.Length - 2; i++)
  50.             {
  51.                 if (st.Length - i < 6)
  52.                     kMax = st.Length - i;
  53.                 for (int k = 2; k < kMax; k++)
  54.                 {
  55.                     if (AandB(st, i, i + k))
  56.                         count++;
  57.                 }
  58.             }
  59.             Console.WriteLine("Amount of substrings: " + count);
  60.             //string[] arr = new string[5];
  61.             //string st;
  62.             //for (int i = 0; i < arr.Length; i++)
  63.             //{
  64.             //    Console.WriteLine("Word no. " + (i + 1));
  65.             //    arr[i] = Console.ReadLine();
  66.             //}
  67.             //Console.WriteLine("Word: ");
  68.             //st = Console.ReadLine();
  69.             //Console.WriteLine(StartsWith(arr, st) + " words start with " + st);
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement