Friday, July 31, 2009

C++ help? (ten points to person with most answers)?

Is there a function in C++ to enter a string into a zip password and check if that string is the correct password?


Second, how can you:


get part of a string


change a string to uppercase


change a string lowercase





Thanks.

C++ help? (ten points to person with most answers)?
There exists no such function.


To get part of a string, you use the substr member function. Hence,


#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


using namespace std;


int main()


{


string s = "Hello World!";


cout %26lt;%26lt; s.substr(3,5) %26lt;%26lt; endl;


return 0;


}


will print out five characters of string s, starting from the character at position 3. The outputted string will be "lo Wo".


To change a string to uppercase: loop through the string and consider each character in turn. If it is greater than or equal to 'a' and less than or equal to 'z', subtract 32 from its ASCII value.


To change a string to lowercase: loop through the string and once again consider each character in turn. If it is greater than or equal to 'A' and less than or equal to 'Z', add 32 to its ASCII value. Hence,


void convert_to_uppercase(string%26amp; s)


{


int i;


for (i=0; i%26lt;s.length(); i++)


if (s[i]%26gt;='a' %26amp;%26amp; s[i]%26lt;='z')


s[i]-=32;


}


void convert_to_lowercase(string%26amp; s)


{


int i;


for (i=0; i%26lt;s.length(); i++)


if (s[i]%26gt;='A' %26amp;%26amp; s[i]%26lt;='Z')


s[i]+=32;


}
Reply:No, there is no function in standard C++ to enter a string into a zip password and check if it is correct. You will have to use a C++ Zip library. I suggest you check out http://www.example-code.com/vcpp/zip.asp


You should be able to convert the MFC examples to C++ with no modification.





Idk about your second question.





Third is:


for(int i = 0;i%26lt;strlen(StringHere);i++)


{


if(StringHere[i] %26lt; 97){StringHere[i] =


StringHere[i]+32;


}


}


Last Question:





for(int i = 0;i%26lt;strlen(StringHere);i++)


{


if(StringHere[i] %26gt;= 97){StringHere[i] =


StringHere[i]-32;


}


}





That function is made from the fact that the lowercase versions of letters in ASCII are always the number of the uppercase + 32. You should be able to put the loops I just posted in a function, so you don't have to write all that everytime you wish to convert to lowercase or uppercase.





http://www.asciitable.com/


No comments:

Post a Comment