noob + C = help

Status
Not open for further replies.

ncjimn

In Runtime
Messages
471
Ok, not that I want you to help me do my HW, but here is my first assignment of C programming.

Feel free to explain as much as you can (it's only like 3 lines of code).

Don't think that I am a slacker or a stupid person, I just think that if someone who knows this stuff explains what's going on I can understand better.

So yeah, some help here.

1. Enter and compile the following C program.

main() {
int i;
char str[255]; // max name length is 255 chars long

printf("Input your first name: ");
scanf("%s", str);

for (i = 0; str != '\0'; i++) {
str = str | 0x20;
printf("%c", str);
}
printf("\n");
}

2. Modify the program in problem 1 to print the user's first name backwards (e.g., "thomas" outputs "samoht").
3. Modify the program in problem 1 to print out the user's name in all upper case (e.g., "thomas" outputs "THOMAS")


Thanks :cool:
 
Haha, these work! I had not used C previously, and this is by no means the easiest / best way of doing this...

1. Dev-C++ is what I used. http://www.bloodshed.net/devcpp.html

2. Reversing the order...there are so many ways, but this is the most obvious to me (again, I am NOT familiar with C): set i to the max string length - 1 (since 0 is a value), loop until i < 0, and subtract i as you go along.
Code:
#include <stdio.h>

main() {
       int i;
       char str[255]; // max name length is 255 chars long

       printf("Input your first name: ");
       scanf("%s", str);

       for (i = strlen(str) - 1; i >= 0; i--) {
           str[i] = str[i] | 0x20;
           printf("%c", str[i]);
       }

       printf("\n");
}

3. To convert to uppercase, you just wrap it with toupper(char):
Code:
#include <stdio.h>

main() {
       int i;
       char str[255]; // max name length is 255 chars long

       printf("Input your first name: ");
       scanf("%s", str);

       for (i = 0; str[i] != '\0'; i++) {
           str[i] = toupper(str[i] | 0x20);
           printf("%c", str[i]);
       }
       
       printf("\n");
}


Thanks for the short-lived entertainment...;)
 
That was kind of useful, but those are C++ subroutines. Which basically aren't what I am looking for. (Thanks anyways Vormund).

C is supposed to be a lower level language, so I basically need the code to actually change the ASCII values of lowercase letters to the ASCII values of uppercase.
 
If you look at the ascii code for a lower-case letter and it's upper case letter, you'll notice that the difference is always 0x20 in hex.

So if you want to convert each letter from lowercase to upper case, you'd subtract 0x20 to each character in the array.

I'm not one for writing out code for people, but if you'd like more explanation I'd be more than happy.
 
jaeusm said:
Those actually are C subroutines.

Well, even if they are (I'm still a noob on this remember?) they're not what I need. We are starting from the bottom up so subroutines (not written by me in this case) aren't an option right now.

@bla

yeah, I knew that and I used it to change the letters to uppercase
I ANDed the input ascii with 0xDF and that did it.
(notice that this works even if the input is in uppercase).

For getting the reversed string I need to start my loop at the end of the string and go back (like Vormund suggested) I am just not familiar with the code for pointers in C. If someone could explain how the pointers work that'd be great!

I guess I have the most trouble figuring out the syntax and knowing what those little crazy "\x, \r, %d" things mean.

I still need to read a few chapters on my book, after reading I should be able to at least know how to complete this assignment.

Thank you all for replying :cool:
 
pointers??? well.....imagine a bank with lockers all over the wall......u own 1 and u wanna put all ur money in it.........what u do ..tryin to be safe and all...is put in another locker just a slip of paper.....WITH THE LOCKER NUMBER of the safe with all the loot! .....u access the safe with the paper...voila u get the loot!

kinda like that....the lockers ...MEMORY locations, the one u put the cash in...ur variable...the one u put ur paper in(tellin u the location of the cash IE the address of the cash)....is the pointer....

Basically the pointer holds the address of a memory location.

if its int pointer, then that memory location it points to, will or must contain an INT variable.

best way to learn it is keep making memory diagrams when ever dealing with pointers.

and learn syntax.
 
Status
Not open for further replies.
Back
Top Bottom