home *** CD-ROM | disk | FTP | other *** search
- /*
- C++ program that demonstrates searching for
- characters and strings
- */
-
- #include <iostream.h>
- #include <string.h>
-
- const unsigned STR_SIZE = 40;
-
- main()
- {
- char mainStr[STR_SIZE+1];
- char subStr[STR_SIZE+1];
- char findChar;
- char *p;
- int index;
- int count;
-
- cout << "Enter a string : ";
- cin.getline(mainStr, STR_SIZE);
- cout << "Enter a search string : ";
- cin.getline(subStr, STR_SIZE);
- cout << "Enter a search character : ";
- cin >> findChar;
-
- cout << " 1 2 3 4\n";
- cout << "01234567890123456789012345678901234567890\n";
- cout << mainStr << "\n";
- cout << "Searching for string " << subStr << "\n";
- p = strstr(mainStr, subStr);
- count = 0;
- while (p) {
- count++;
- index = p - mainStr;
- cout << "Match at index " << index << "\n";
- p = strstr(++p, subStr);
- }
- if (count == 0)
- cout << "No match for substring in main string\n";
-
- cout << "Searching for character " << findChar << "\n";
- p = strchr(mainStr, findChar);
- count = 0;
- while (p) {
- count++;
- index = p - mainStr;
- cout << "Match at index " << index << "\n";
- p = strchr(++p, findChar);
- }
- if (count == 0)
- cout << "No match for search character in main string\n";
- return 0;
- }
-