PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
reverse_string.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 
31 /** \file
32  * Inputs a string, outputs the reverse. This file is used
33  * in three MPLAB projects:
34  * reverse_string.mcp - polled RX, TX I/O
35  * uartrx_fifo.mcp - interrupt RX, polled TX I/O
36  * uartrxtx_fifo.mcp - interrupt RX, interrupt TX I/O
37  * Interrupt RX inChar1() is selected by defining UART1_RX_INTERRUPT macro
38  * Interrupt TX outChar1() is selected by defining UART1_TX_INTERRUPT macro
39  * These macros are defined in their respective MPLAB projects.
40 */
41 
42 void reverseString(char *psz_s1, char *psz_s2) {
43  char *psz_s1end;
44  if (!(*psz_s1)) {
45  *psz_s2 = 0; //psz_s1 is empty, return.
46  return;
47  }
48  psz_s1end = psz_s1;
49  //find end of first string
50  while (*psz_s1end) psz_s1end++;
51  psz_s1end--; //backup one to first non-zero byte
52  //now copy to S2 in reverse order
53  while (psz_s1end != psz_s1) {
54  *psz_s2 = *psz_s1end;
55  psz_s1end--;
56  psz_s2++;
57  }
58  //copy last byte
59  *psz_s2 = *psz_s1end;
60  psz_s2++;
61  //mark end of string
62  *psz_s2 = 0;
63 }
64 
65 
66 #define BUFSIZE 63
67 char sz_1[BUFSIZE+1];
68 char sz_2[BUFSIZE+1];
69 int main (void) {
70  configBasic(HELLO_MSG);
71  while (1) {
72  inString(sz_1,BUFSIZE); //get a string from the console
73  reverseString(sz_1, sz_2);
74  outString(sz_2); //output the reversed string
75  outString("\n");
76  }
77 }