Advertisement
Tiger6117

String Finder

Jun 11th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. /* This Program is for String Finder . i.e : Write a program that takes a string from user and search a word in the string. The program Prints position of 1st and last letter of the word in the string if it finds the word !!!! */
  2.  
  3. // For more scripts and tricky stuff visit : www.tigerzplace.com
  4.  
  5. //Program Code:
  6.  
  7. #include <iostream>
  8. #include <conio.h>
  9.  
  10. using namespace std;
  11.  
  12. int search(char str[], char sbstr[])
  13. {
  14.     int subl,strl;
  15.     for (int x=0;str[x]!='\0';x++)
  16.     {
  17.         strl=x;
  18.     }
  19.     for (int y=0;sbstr[y]!='\0';y++)
  20.     {
  21.         subl=y;
  22.     }
  23.  
  24.  int c=0,f=0,l=0,cnt=-1;
  25.         for(int s=c;s<=strl;s++)
  26.          {
  27.         if(sbstr[c]==str[s])
  28.         {
  29.  
  30.                 if (c==0 || c==subl)
  31.             {
  32.  
  33.                 switch(c)
  34.                 {
  35.                 case 0:
  36.                      f=s+1;
  37.                   break;
  38.                 default:
  39.                     l=s+1;
  40.                    break;
  41.                  }
  42.  
  43.             }
  44.             cnt++;
  45.             c++;
  46.  
  47.         }
  48.          }
  49. if(cnt==subl)
  50. {
  51.     cout << "SubString Found!!!\n";
  52.     cout << "Location First : "<<f;
  53.     cout << "\nLocation last :"<<l;
  54. }else{
  55. cout << "SubString Not found !!!! \n";
  56. }
  57.  
  58.     getch();
  59. }
  60. int main()
  61. {
  62.     char str[50],sbstr[48];
  63.     cout << "\t\tSub String Finder\n\n";
  64.     cout << "NOTE::::: Spaces are not allowed !!! ";
  65.     cout << "\nEnter Your String : ";
  66.     cin >>str;
  67.     cout << "\nEnter the substring you want to find :";
  68.     cin>>sbstr;
  69.     search(str,sbstr);
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement