![]() |
|
#1
|
|||
|
|||
C-String pointersgood morning!
please put some comments in this code, this program will convert C-String from lower case to upper case using pointer notation. only the iostream header file is allowed to use. here is the code: CPP / C++ / C Code:
This code is actually working but i need some comments/suggestions to improve this code. thanks! |
|||
|
#2
|
|||
|
|||
Re: C-String pointersQuote:
In programming, even if it seems to work, there are issues of style that affect its suitability. Note that "suitability" may be defined by someone who gives a grade or signs a paycheck, so "suitability" may be quite subjective. Please accept the criticism in the spirit in which it was offered: an attempt to help, and not an attempt to put you (or your code) down. The fact that you presented a functional program is a Good Thing, and I congratulate you for your effort. Also note that my comments are mostly about style, and maybe other people have different opinions. Everyone is entitled to his/her opinion, and, in fact, my own opinions change from time to time. I welcome any comments about my comments. And you did ask... CPP / C++ / C Code:
Regards, Dave Footnote: Actually, I think the point she was trying to get across is the contrapositive: If it doesn't work, I don't care how "pretty" it looks, it isn't pretty. I think your code works as you expected it to work, but you did ask... |
|
#3
|
|||
|
|||
Re: C-String pointershi, i was reading through your comments please would you go over these statements
CPP / C++ / C Code:
1- what is the zero byte ? 2-how do you append the zero byte? thanks all. |
|
#4
|
|||
|
|||
Re: C-String pointersQuote:
I respectfully suggest that you look in a book or look for an on-line tutorial or other reference on C-style "strings." For example: C Strings Tutorial or C String - Wikipedia However, I note that there is an unfortunate typo in the Wikipedia article. The ASCII char for a byte with value zero is called NUL, not NULL. The identifier NUL means nothing to C++, but NULL is defined as a pointer. NULL is not part of the base language itself, but is a #defined macro in several standard headers that programs are likely to use. Just to add to the confusion, In C++, NULL denotes "an implementation-defined C + + null pointer constant" according to Annex E of the C++ Standard Language document ISO/IEC 14882:2003. I mentioned my objection to the inappropriate use of NULL in the Original Poster's code. Sometimes, in descriptions of C-style "strings," a zero-byte is called a "null character," but note that it is not NULL. Programs should be more precise, and programmers and writers should not confuse narrative descriptions with C/C++ identifiers. Anyhow... The C programming language does not have a string data type. C-style "strings" consist of a sequence sequence of chars in memory, terminated by a char with all its bits equal to zero. (Therefore, a C-style "string" can not have a byte equal to zero inside the "string".) Why is this important in C++? Well if you have an array of chars (or a pointer to char), and if you do something like "cout<<", as I showed in my main() function, the "<<" operator is overloaded to expect a C-style string. That is, it prints characters and won't stop printing characters until it sees the zero byte. The zero byte is part of the "string," that is, it occupies a memory location, but, obviously it doesn't sent to whatever output device is the target of the "<<" operator. All standard library functions that work with C-style "strings" have arguments that are pointers to char. The value of the pointer is expected to be the address of a contiguous block of memory containing a sequence of chars. The functions know where the end of the "string" is since there must be a zero byte. (Otherwise, the behavior is undefined.) . Quote:
CPP / C++ / C Code:
Regards, Dave Footnote: In C++, there is a std::string class that is probably more appropriate than C-style "strings" for many applications. People who learn C first should be greatly relieved to discover the usefulness and relative safety of C++ std::strings compared with the outdated-but-not-quite-obsolete-since-there-are-still-places-where-they-are-useful C-style "strings." People who learn C++ without ever being exposed to C-style "strings" are sometimes amazed to learn that C doesn't have a "string" data type, and some legacy functions and applications require the zero-byte terminated idiom. |
|
#5
|
|||
|
|||
Re: C-String pointersCPP / C++ / C Code:
this statement will terminate if null character is encounter. how if a character array consist of more that 1 c-string what would be the condition of the loop so that it will terminate when the end-of-file is encounter? CPP / C++ / C Code:
|
|
#6
|
|||
|
|||
Re: C-String pointersQuote:
Quote:
A sequence of non-zero bytes followed by a zero byte, followed by a sequence of non-zero bytes followed by a zero byte. It is certainly possible to have this in your array, but "normal" C-string handling functions (including "cout <<") will not be able to handle it. It is possible to write a legal program that uses this kind of stuff, but I'm not sure why you want to do so. (That doesn't mean that I think you should not do it, but in order to try to help, I would like to know what you really have in mind.) Quote:
Bottom line: results of a comparison of a char with a "multi-character character constant" are undefined. Program behavior is undefined and unpredictable. Maybe you had in mind the implementation-dependent defined constant EOF that is used in some file operations. Typically EOF is defined (somewhere in some header file) to be an unsigned integer with a value of -1 (but not always). Suppose that, somehow, you put a special "sentinal" character at the end of your two c-strings so that the comparison causes the loop to stop when it gets there, and not when it gets to a zero byte. Then what? I mean , what are you going to do with the results? How will you use (print or what) the resulting stuff? You won't be able to use "cout<<", since output will stop when it sees the first zero byte. So, my questions for you are; 1. How do you propose to get multiple strings in your array of chars? I mean, I can think of some ways to do it, but I don't think I want to do it, and I would like to know what you have in mind. 2. How do you propose to get the special "sentinal" byte at the end of the array. Regardless of your answers (or further questions) about items 1 and 2, here's the biggie: 3. What is it that you really are trying to accomplish beyond the the requirements that you originally posted? After all, you stated that it was working and asked for comments. Nothing that I suggested changed the final results of your working program. On the other hand, your attempted "enhancement" royally screws the pooch. Regards, Dave |
|
#7
|
|||
|
|||
Re: C-String pointersthere were no problems about the original post!!
i modified it by following some of your comments, and it was good!! Now, this in not an assignment or project, ect. i'm just practicing my understanding about this matter. so i trying to modify my original code, but this time since 32 is the key to convert a lower case to upper case or simply ( 'a' - 'A' ), i replaced it by a variable type int and use that value as the encryption key. this program will read from a text file then output the encrypted value to another text file. 1. read the encrytion key 2. put all the c-string into an array of characters 3. copy all c - string with its ecrypted form to another array of character 4. display the encrypted character. this code is working with other value except 32. CPP / C++ / C Code:
why is that, the other value encrypts all the characters, but with 32 only the first c-string is encrypted.. ( in this case from lower to upper case ) |
|
#8
|
|||
|
|||
Re: C-String pointersQuote:
Quote:
The integer value of an ASCII space char is 32. When the translation function sees a space in the input array, it writes a zero byte at that point in the output array (' ' - 32 is equal o zero). On the other hand, If you choose a key of 44, it will write a zero byte into the output array whenever the input character is a comma, since the integer value of the ASCII comma character is 44. Etc. Now your function continues with the translation beyond that point up until it finds a zero byte in the source array. And, by the way, here is a problem: The fin.get() operation does not put a zero byte at the end of the input. It stops when it has read as many chars as you tell it or when it reaches the end of the file. unlike the istream ">>" operator, the get() function is not a c-style "string" function, and does not put the terminating byte at the end of the input. I'll show a way to get around this in a minute. Now for the output: As I mentioned previously, the "<<" operator for an output stream treats an array of chars as a C-style string. It writes characters and it stops when it gets to a zero byte (it doesn't write the zero byte). Here is a program (that uses some things that you may not have covered yet) to show the contents of the arrays. Each line consists of the following: 1. An index into the arrays. 2. The character in the data array. Note that the newline at the end of a line is not a "printing" character, so I print the integer value in hexadecimal. 3. The decimal integer value of the character in the data array. 4. The decimal integer value of the character in the encryptCopy array. CPP / C++ / C Code:
Code:
The output file consists of exactly six bytes, since the "fout << " statement stopped after it wrote the byte whose value is 12 (the converted comma), since the next char in the output array is a zero byte. The program not only lets me show you what happens with this particular file, it also shows ways of printing integer values of characters. It shows how to print hexadecimal values of integers. It shows a way to make your output line up, using setw(). It shows how to print leading zeros by using setfill(). When people are just exploring and not trying to create a polished "report" as the output, sometimes they just print any old thing without regard to how it looks (lining up the items in specific columns, for example). I like to make sure that I know how to get it looking good, because someday I may want to create something like this for my boss. (Or sometimes I think of my mom. She always liked to hang my artwork on the refrigerator, even if it wasn't very good, but I liked to try to make it look nice for her. I like to imagine her hanging my computer output on the refrigerator.) Maybe that is more than you wanted to know. (OK, so ignore it for now---maybe you will get back to it some day.) On the other hand, maybe it is less than you wanted to know. With a little knowledge of ways to print out values that the program is seeing, you can explore more. Of course it's always OK to ask questions about stuff that you don't understand. Bottom line: When you leave the well-paved path of structured educational presentation from class or from the carefully crafted expositional sequence in a book, you can sometimes run into things that require some extra work in order to understand, but if you are really interested, you may find that it will come in handy later. Regards, Dave Footnote: If you want to write all of the elements of the output array, you can open the file in binary mode and use fout.write() to do the deed. You can not use "<<" to write an array of chars that has zero bytes embedded in the array. Period. |
Recent GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Message Class | TransformedBG | C++ Forum | 5 | 29-Nov-2006 22:28 |
| Help wit my source code compiler errors | Krandygrl00 | C++ Forum | 1 | 06-Jun-2005 09:14 |
| [Tutorial] Pointers in C (Part II) | Stack Overflow | C Programming Language | 0 | 27-Apr-2005 18:36 |
| [Tutorial] Pointers in C (Part I) | Stack Overflow | C Programming Language | 1 | 08-Apr-2005 19:35 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The