Ads 468x60px

Sunday 11 December 2011

C++ program that returns the position for a substring

This is a C++ program that accepts string from user and search for a sub string (group of continuous characters). If the search is successful, the function should return the position of sub string and otherwise -1.
The program coding can be given as:

#include #include #include // Function to search a given string within a string and its position int search(char str[20], char substr[10]) { int j = 0, i, flag = 0, pos; for(i=0; str[i]!='\0'; i++) { if (substr[j] =='\0') break; if (str[i] == substr[j] ) { pos = i; flag = 1; if (substr[j] =='\0') break; j++; } else flag = 0; } for(j=0; substr[j]!='\0'; j++); if (flag == 1) { pos = (pos - j + 2); return(pos); } else return(-1); } void main() { char nstr[20], nsub[10]; int ctr = 0; cout << "Enter main string : "; gets(nstr); cout << "Enter search string : "; gets(nsub); ctr = search(nstr, nsub); if (ctr == -1) cout << "String does not found."; else cout << "String found at " << ctr << " position."; }
Source:  www.LearnCandCpp.com

No comments:

Post a Comment