Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** A bit of code which will ask you to enter a string and then it'll
- * write it in reverse. MMXII Donkeysoft */
- // Unfortunately, I used Visual Studio 2010, so you need to add the following
- // in place of this, or in the StdFxa.cpp file:
- // #include <stdio.h>
- // #include <stdlib.h>
- // #include <conio.h>
- // #include <string.h>
- #include "stdafx.h"
- // Function prototypes:
- void main (void);
- void clear (void);
- static void reverse (void);
- // Global variables:
- static int i=0;
- static int count=0;
- static char word[255];
- static char reversed[255];
- static int nChars;
- static char key=' ';
- void main(void)
- {
- // Does a cheat for clearing the console:
- clear();
- printf("Enter a word or sentence to be reversed (max is 255 characters): ");
- scanf("%s", word);
- printf("\n");
- // Calls the function to reverse the string:
- reverse();
- i=0;
- // Writes each character while not null:
- while(word[i])
- {
- printf("%c",word[i]);
- i=i+1;
- }
- printf(" reversed is %s\n",reversed);
- printf("\nPress any key to exit.");
- // Sets key to zero:
- key=0;
- // Repeats until a key is pressed, or in other words, waits until a key is pressed:
- while(!key)
- {
- key=getchar();
- }
- }
- // Clear console function:
- void clear(void)
- {
- // 120 newlines:
- for(i=0;i<120;i=i+1)
- {
- printf("\n");
- }
- }
- // Reverse function:
- void reverse(void)
- {
- nChars=0;
- // Counts number of characters:
- while(word[nChars])
- {
- nChars=nChars+1;
- }
- printf("Word length is %d\n",nChars);
- // The last character is null, so we don't need that:
- nChars=nChars-1;
- // Writes the indexed string at reversed [0+i] to the indexed string word [char-i]
- for(i=0; i<=nChars+1; i=i+1)
- {
- reversed[i]=word[nChars-i];
- }
- // Adds a null value to say that this is the end of the string:
- reversed[i]='\0';
- // Should clear keyboard buffer:
- getchar();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement