#include <iostream>
#include <string>
using namespace std;
int searchInStr(const string &str, char ch, int start, int end) {
for (int i = start; i <= end; i++) {
if (str[i] == ch) {
return i;
}
}
return -1;
}
int main() {
string str = "Java Programming";
char ch;
int start, end;
cout << "Enter character to search: ";
cin >> ch;
cout << "Enter the range,\n";
cout << "Enter the start index: ";
cin >> start;
cout << "Enter the end index: ";
cin >> end;
int res = searchInStr(str, ch, start, end);
if (res == -1) {
cout << "Character not found in the specified range" << endl;
} else {
cout << "Character is at index " << res << endl;
}
return 0;
}
Program: Searching for a character in specified range.
2February 21, 2025 9.2K 1