Advertisement
Maxa

ARDUINO /// Palindrom

Dec 14th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. /*
  2.   Serial Event example
  3.  
  4.   When new serial data arrives, this sketch adds it to a String.
  5.   When a newline is received, the loop prints the string and clears it.
  6.  
  7.   A good test for this is to try it with a GPS receiver that sends out
  8.   NMEA 0183 sentences.
  9.  
  10.   NOTE: The serialEvent() feature is not available on the Leonardo, Micro, or
  11.   other ATmega32U4 based boards.
  12.  
  13.   created 9 May 2011
  14.   by Tom Igoe
  15.  
  16.   This example code is in the public domain.
  17.  
  18.   http://www.arduino.cc/en/Tutorial/SerialEvent
  19. */
  20.  
  21. String inputString = "";         // a String to hold incoming data
  22. boolean stringComplete = false;  // whether the string is complete
  23. int strLen = 0;
  24.  
  25. String poruka = "Duzina stringa: ";
  26. String poruka1 = "Uneli ste: ";
  27.  
  28. void setup() {
  29.   // initialize serial:
  30.   Serial.begin(9600);
  31.   // reserve 200 bytes for the inputString:
  32.   inputString.reserve(200);
  33. }
  34.  
  35. void loop() {
  36.   // print the string when a newline arrives:
  37.   if (stringComplete) {
  38.    
  39.     strLen = inputString.length() - 1; // ne racunam \n karakter
  40.    
  41.     Serial.println(poruka1 + inputString);
  42.     Serial.println(poruka + strLen);
  43.  
  44.     // proveravam palindrom
  45.     proveriPalindrom();
  46.    
  47.     // clear the string:
  48.     inputString = "";
  49.     stringComplete = false;
  50.   }
  51. }
  52.  
  53. void proveriPalindrom(){
  54.   String curr = "";
  55.   String suprotni = "";
  56.   boolean jestePalindrom = true;
  57.   for( int i = 0; i < strLen / 2; i++) {
  58.       curr = inputString.charAt(i);
  59.       suprotni = inputString.charAt(strLen - 1 - i);  // karakter na simetricnoj poziciji
  60.       //Serial.println(curr);
  61.  
  62.       //Serial.println(" Trenutni: " + curr);
  63.       //Serial.println("i = " + 1 + " Trenutni: " + curr);
  64.  
  65.       Serial.println("Suprotni: " + suprotni);
  66.       if(curr != suprotni) {
  67.         jestePalindrom = false;
  68.       }
  69.   }
  70.  
  71.   if(jestePalindrom) {
  72.       Serial.println("Uneseni string jeste palindrom!");
  73.   } else {
  74.       Serial.println("Uneseni string NIJE palindrom!");  
  75.   }
  76. }
  77. /*
  78.   SerialEvent occurs whenever a new data comes in the hardware serial RX. This
  79.   routine is run between each time loop() runs, so using delay inside loop can
  80.   delay response. Multiple bytes of data may be available.
  81. */
  82. void serialEvent() {
  83.   while (Serial.available()) {
  84.     // get the new byte:
  85.     char inChar = (char)Serial.read();
  86.     // add it to the inputString:
  87.     inputString += inChar;
  88.     // if the incoming character is a newline, set a flag so the main loop can
  89.     // do something about it:
  90.     if (inChar == '\n') {
  91.       stringComplete = true;
  92.     }
  93.   }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement