Monday, May 24, 2010

Simple C++ question?

i am trying an example from a book and i cant quite get it yet. hope you can help me.





Question: DECLARE A CHARACTER ARRAY, AND INITIALIZE IT TO A SUITABLE STRING. USE A LOOP TO CHANGE EVERY OTHER CHARACTER TO UPPERCASE. (HINT: ASCII UPPERCASE COUNTER PARTS ARE 32 LESS THEN THERE LOWERCASE )





so i have to use this arithmetic way no. no built in function in a library.





this is what i have so and cant figure out.





#include %26lt;iostream%26gt;





using namespace std;





int main()


{


char c[] = "hello world!";





int count = 0;





for( int i = 1; i %26lt; count; i = i + 2)


{


count++;


c[i] = i - 32;


cout %26lt;%26lt; c[i];


}


//cout %26lt;%26lt; c;








return 0;


}





i thought i have everything right except the cout %26lt;%26lt; part however you do it.





any help or suggestions..thanks

Simple C++ question?
first of all,you have wrongly declared the counter variable count.


The loop will exit after one pass. Also, it should be c[i]-=32; or


c[i]=c[i]-32;





try this code instead of the for loop


i=0;


while(c[i]!='\0')


{


c[i]-=32;


i++;


}
Reply:The answers above is somehow incorrect, but some users have suggested good stuff.





I am gonna present you with a solution similar to yours, but I have seen some indexing problems with yours, you have to realize an array starts at POSITION 0 not 1, and in the for loop, you just place i++ to increment to the next position from the character array.





But you have to realize that there are incorrect strings and incorrect symbols, a proper way is to do the following:





http://rafb.net/p/GoMp5X43.html


===========================


char c[] = "hello $^^#67world!";





int count = sizeof(c) - 1;





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


{


// Now only convert when it is a small character


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


}





cout %26lt;%26lt; c;


===========================





That will print out the correct line, and all the symbols will stay intact, so "Hello World!" = "HELLO WORLD!"





Now notice the condition i placed? It should be between a to z.





Now that should work correctly since I tested it here.





Good Luck :)
Reply:What do you mean by "can't figure it out"... you need to say what your problem actually is. The code you have posted seems to be a strange mix of C# and C++... this is not going to work, you need to use one language.





"using" is a C# thing. And you aren't using it correctly anyway. The C# "using" statement when used as a precompiler directive, will import a library in the same way that #include did for C++ and C languages.





These are the things you need to think about for this:


1. what to do if a letter is already uppercase


2. how to determine the end of the string (is it null-terminated, or length-prefixed)


3. How to deal with numbers, whitespace, and control characters.





You have an error in your loop. Look at what you did very carefully. First, you set a variable called 'count' to be zero. Then, you declare a loop and a new integer called 'i', and you initialize 'i' to be 1. Then you say that the loop condition is supposed be "i less than count", which is false the first time in, so the loop never runs.
Reply:One little thing to beware of is that you will also be subtracting


32 from the space when i = 5. This will convert that space into a null, which will prematurely terminate the string.


No comments:

Post a Comment