Advertisement
JuanTorres

Untitled

Jan 27th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. const int size = 4;
  6.  
  7. int linearSearch(char[], char&, int);
  8.  
  9. int main()
  10. {
  11.     char name[] = { 'J', 'u', 'a', 'n' };
  12.     char letter;   
  13.  
  14.     for (int i = 0; i < size; i++)
  15.     {
  16.         cout << name[i] << " ";
  17.  
  18.     }cout << endl;
  19.  
  20.     cout << "Find a letter of the name :"; cin >> letter;
  21.  
  22.     int x=linearSearch(name, letter, size);
  23.  
  24.     if (x == -1)
  25.     {
  26.         cout << "Not found !" << endl;
  27.     }else
  28.     {
  29.         cout << "Letter found!" << endl;
  30.  
  31.         cout << "Letter :" << letter << endl;
  32.         cout << "Position :" << x << endl;     
  33.     }
  34.  
  35.     return 0;
  36. }
  37. int linearSearch(char list[], char& letter, int size)
  38. {  
  39.     int posi;
  40.  
  41.     for (int i = 0; i < size; i++)
  42.     {
  43.         if (list[i] == letter)
  44.         {          
  45.             return posi = i;           
  46.         }      
  47.     }  
  48.     return posi = -1;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement