Advertisement
GraionDilach

Mintakeresés stringben

Feb 17th, 2012
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 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.         static void Main(string[] args)
  11.         {
  12.             Console.WriteLine("Kérem a mintát.");
  13.             String Base = Console.ReadLine();
  14.             Console.WriteLine("Kérem a keresendő szöveget belőle.");
  15.             String SearchFor = Console.ReadLine();
  16.  
  17.             int Hoooh = ImAnIdiot(SearchFor, Base);
  18.  
  19.             if (Hoooh != -1)
  20.             {
  21.                 Console.WriteLine("A keresett szöveg a " + (Hoooh + 1) + ". helyen kezdődött.");
  22.             } else {
  23.                 Console.WriteLine("A keresett szöveg nem volt benne a sorban.");
  24.             }
  25.  
  26.         }
  27.  
  28.         static int ImAnIdiot(string SearchFor, string SearchIn)
  29.         {
  30.             for (int i = 0; i < SearchIn.Length; i++)
  31.             {
  32.                 if (SearchIn[i] == SearchFor[0]) {
  33.                     for (int j = i, k = 0; k < SearchFor.Length; j++, k++)
  34.                     {
  35.                         if (SearchIn[j] != SearchFor[k])
  36.                         {
  37.                             break;
  38.                         }
  39.                         if (k == SearchFor.Length - 1)
  40.                         {
  41.                             return i;
  42.                         }
  43.                     }
  44.                 }
  45.             }
  46.             return -1;
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement