PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
reverse_string_stdio.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 #include "pic24_all.h"
30 #include <stdio.h>
31 
32 /** \file
33  * Inputs a string, outputs the reverse. This file demonstrates use of
34  * 'scanf' from stdio. The project uses
35 */
36 
37 void reverseString(char *psz_s1, char *psz_s2) {
38  char *psz_s1end;
39  if (!(*psz_s1)) {
40  *psz_s2 = 0; //psz_s1 is empty, return.
41  return;
42  }
43  psz_s1end = psz_s1;
44  //find end of first string
45  while (*psz_s1end) psz_s1end++;
46  psz_s1end--; //backup one to first non-zero byte
47  //now copy to S2 in reverse order
48  while (psz_s1end != psz_s1) {
49  *psz_s2 = *psz_s1end;
50  psz_s1end--;
51  psz_s2++;
52  }
53  //copy last byte
54  *psz_s2 = *psz_s1end;
55  psz_s2++;
56  //mark end of string
57  *psz_s2 = 0;
58 }
59 
60 
61 #define BUFSIZE 63
62 char sz_1[BUFSIZE+1];
63 char sz_2[BUFSIZE+1];
64 int main (void) {
65  configBasic(HELLO_MSG);
66  while (1) {
67  scanf("%s",sz_1); //get a string from the console
68  reverseString(sz_1, sz_2);
69  printf("%s\n", sz_2); //output the reversed string
70  }
71 }