Need Urgent help in code optimization
May 13, 2013 at 7:40am UTC
Hello Forum,
I am not very familiar with string class. Following is the sample code I have written. It works fine but I think I need a lot of optimization in it. Could somebody please help? I need to reduce conditions, time of execution and make it more compact. Thanks in advance :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <iostream>
#include <string>
using namespace std;
int main()
{
string digits;
string b_party_number;
int ton,npi;
cout << "enter the number" <<endl;
getline(cin,digits);
if (digits.compare(0,2,"00" ) == 0)
{
cout << "Number is in 15 digits INT format" << endl;
ton = 4;
npi = 1;
b_party_number = digits;
}
else
{
if (digits.compare(0,1,"8" ) == 0)
{
cout <<" Number is in 14 digits NSN format" << endl;
ton = 3;
npi = 1;
b_party_number = digits.substr(1);
}
else
{
cout << "Number is in 11/12/13 digits INT format" << endl;
//INT format, 1st character is 9.
size_t found = digits.find_first_not_of("0" ,1);
if (found != string::npos)
{
b_party_number = digits.substr(found);
ton = 4;
npi = 1;
}
else
{
cout << "pattern NOT found " << endl;
}
}
}
cout << "Number obtained is " << b_party_number << ", ton is: " <<ton<<"," << "npi is: " << npi << endl;
return 0;
}
May 13, 2013 at 1:54pm UTC
why do you think this few lines need optimization?
what is it supposed to do?
Topic archived. No new replies allowed.