Monday, May 31, 2010

Word Reverse in C.

Hello, We've got another problem or should we say a challenge to solve coming from a site which I've been searching for puzzle and stuff. And suddenly I've seen this problem that reminds me of my Freshmen days when I was still on college. I remember the days when I was like. "Whoa?!, what is this stuff?". We'll that's my first impression on Turbo C. So Antique. But my late professor says, "If you want to be a good programmer, Don't forget the basic". And I will never forget that. All hail Prof. Juanito Doctor of De La Salle University DasmariƱas!

Here's the Problem:

Write a program that inputs a string of keyboard characters and outputs the characters in reverse order.

We'll, It's string manipulation time. We will use Arrays and Conditionals to solve this. We need to Exchange the first character to the last character of the string. Then the second character to the second to the last character of the string and etc. Like this....




So now. Since we know how to do that. It's time for the coding!

First Let's declare the Header files.

#include <stdio.h>
#include <string.h>

We need to include those. Those are the basic Header file for input and output which is stdio.h which means, Standard Input and Output while the string.h is responsible for string manipulation.

main()
{
int ctr,i;
char z[99],temp;
clrscr();

We need to declare the variables for this program as well. Now notice the "z" variable which has a number [99]. Note: It is not a function! It is an array. A one dimensional array to be exact. And the 99 denotes the size of the array. so we can say that...

z[0,1,2,3,4,5,...99].

And we can input on every index on "z".

z[0] = 1
z[1] = 33
z[2] = 5

Just be specific on the variable type. And in this case, The z variable type is a Character.

gets(z);

Now, This is the manner on how we get the input from the user with Strings. The "gets" function, or Get String Function is on the header file string.h. Unlike Scanf Function which is commonly used in inputting integers, The gets string function takes all inputs even there is a "space" character between the input.

On scanf if we enter "dog trainer", The variable will only have the "dog". Cutting the string if the scanf sees a space character. While on gets, If we input "dog trainer, The variable will have "dog trainer" as it is.

for(ctr = 0; ctr<=strlen(z)-1; ctr++)
{
if(ctr < strlen(z)/2)
{
temp = z[ctr];
z[ctr] = z[strlen(z)-1-ctr];
z[strlen(z)-1-ctr] = temp;
}
}
Now, This code does the magic. The strlen function is the function that indicates how much characters we inputted on our variable. The for loop indicates to loop the procedure until the end of string is reached. And for the If function, It simply invokes that we will only do the procedure if we reached the half of the string. And if it satisfies the condition, The exchange will occur between the first index to the last index, then second index to the second to the last index and so on.

You see,

on our example BLOGER, We only need to do the exchange 3 times. Which means, we only need to do the exchange operation half the times the number of the characters on the string.


for(ctr = 0; ctr<=strlen(z)-1;ctr++)
printf("%c", z[ctr]);
getch();

Now this code is simply to show the new value of "z". after the exchange procedure.

Well done! We've done the Word Reverse program in C! Ahhhhh... Another nostalgic program. To sum it up. Heres the code.

#include <stdio.h>
#include <string.h>
main()
{
int ctr,i;
char z[99],temp;
clrscr();
gets(z);
for(ctr = 0; ctr<=strlen(z)-1; ctr++)
{
if(ctr
{
temp = z[ctr];
z[ctr] = z[strlen(z)-1-ctr];
z[strlen(z)-1-ctr] = temp;
}
}

for(ctr = 0; ctr<=strlen(z)-1;ctr++)
printf("%c", z[ctr]);
getch();


}


Anyway, Let's see a sample run!

To download this C File. Click
Here. Once again, Thank you for viewing my tutorial. All Hail Alma Mater Hail to De La Salle! Animo La Salle!


Good day and God Bless.

[/code]

No comments: