I'm trying to learn C++ and have an exercise to perform a case sensitive/insensitive search on a string and return the position if found. We were given the function prototype - up to me how I implement it. Im trying to get sensitive part to work first. This is what I have so far. Question I have is why does my else statement retun garbage and not npos which is -1?
Although that value seems odd it isn't, you are getting that value because of how string::npos holds the value of -1. And due to the fact that your function returns a type of size_t, an unsigned integer, it's ultimately represented by wrapping around from the maximum size to hold the value. In simple terms, since the number is negative, it will show itself as a positive by using the maximum value size_t can hold + 1 minus the value.
Change the type of size_t to int and you'll see npos being -1. Likewise, try returning negative values differing from -1 to see its effects.
Generally speaking you should check if result is equal to -1 and if so print it otherwise don't or manually print -1.
As an aside, if you stick with programming, you'll start to see certain numbers come up often enough that you recognise they're meaningful in binary and thus some kind of special value that gives you a clue.
Excellent - thanks for your help. I was on for a quite while wondering why I was getting 18446744073709551615 returned. I have had anothe go below. Still a bit of fine tuning to go but its getting there.
#include <iostream>
#include <string>
#include <algorithm>
enumclass Case{SENSITIVE, INSENSITIVE};
size_t Find(
const std::string &source,//Source string to be searched
const std::string &search_string,//The string/char to search for
Case searchCase = Case::INSENSITIVE,//choose whether search is case senisitive or not
size_t offset = 0){ //start the search from this offset
std::string search_string_toupper = search_string;
std::transform(search_string_toupper.begin(),search_string_toupper.end(),search_string_toupper.begin(), ::toupper);
auto position = source.find(search_string, offset);
auto upper_position = source.find(search_string_toupper, offset);
//always carry out case sensitive search first, will return if found
if(position != std::string::npos){
return position;
}//if not found above and requested INSENSITIVE search using transformed string, return if found
elseif(position == std::string::npos && searchCase == Case::INSENSITIVE){
return upper_position;
}
//return if neither above is true
return std::string::npos;
}
int main(int argc, constchar * argv[]) {
std::string input {"Hello World"};
std::string search{"w"};
auto npos = std::string::npos;
auto result = Find(input, search, Case::SENSITIVE, 0);
if( result == npos){
std::cout << "Not Found" << std::endl;
}
else{
std::cout << "Found at position: " << result << std::endl;
}
//std::cout << npos << std::endl;
return 0;
}