Tag Archives: palindrome

plaindrom_c++_mycomsats

Palindrome Program in C++

In this tutorial (Palindrome Program in C++) we going to talk about a C++ program to check the string if it is palindrome or not. The program used for executing this code is Microsoft Visual Studio which has many version last one is 2011.Visual studio can be used to create Windows applications (or Desktop applications) and also we can create console applications, which use a black window (Command Line Interface cmd) for viewing and getting data to and from user.The code next will be a console application, where user will have to enter the string to be checked.

  • The idea of the program is simply to check the similarity of the first and last characters, then the second and the before last character and so on.
  • If the program found that all comparisons are equal, it will output: Palindrome, otherwise it will output:Not Palindrome.

Palindrome Program in C++ Code

#include#includeint main(){char strn[80];cout<<"Enter the string: ";cin.getline(strn,80);int len=strlen(strn);        bool flag=true;  // create a Boolean value, "flag" to be used in our loopfor(int c=0;c!=len/2;c++) // do a loop from 0 to half the length of the string{if(flag) // if it is a palindrome so far{if(strn[c]!=strn[len-c-1]) // check the characters match{flag=false; // if they don't set the indicator to false}}else{break; // if it is not a palindrome, exit the for loop}}// if flag is true cout "Palindrome" otherwise output "Not Palindrome"if(flag){cout<<"Palindrome";}else{cout<<"Not Palindrome";}cin.get();return 0;}

Palindrome Program in C++ Explanation

#include#include

These includes some of the built-in functions in C++, where:- iostream is to include the functions of input and output which are in this program: cin and cout.- string is to enable us to use strings in the program as string is not a data type in C++ and it should be included at the start of the program.

int main()

This is our main function, where any C++ program should have one and only one main function which is the heart of the program.You can write the whole program in the main as we did here, or you can write many functions out of the main, but you will HAVE TO call them from main.

{char strn[80];

This is a declaration sentence, where in this case we declare a variable of type character, but in this case it is an array of 80 characters as maximum length named strn.

cout<<"Enter the string: ";

This line outputs the sentence Enter the string: on the console interface and this is what cout keyword is used to.

cin.getline(strn,80);

This line is to input the string from the user through the console interface, and this is what cin.getline is used to.

int len=strlen(strn);

This line declares a variable of type integer namedlen.The value of len is the length of the strn which we entered previously.

bool flag=true; 

Creates a variables of type Boolean, which takes only one of two values, either true or false.

for(int c=0;c!=len/2;c++){

This initiates for loop which is a way to execute a specific part of the code more than one time, and it works as follows: First we use a variable which is c in this example. armbandjes kinderen . training solutions . We define a starting and ending value of c and a value for it to use it as increment.In this case we will initiate c with the value zero, and as c didnt reach the final value which will be the half of the length of the string, it continues the loop with increment of one ++.

if(flag) 

If statement is used to check a condition: if the result is true, it executes the part of code in the next braces, which will be done here as the value of flag is true . design bbq .

{if(strn[c]!=strn[len-c-1]) 

This statement is to check the characters match, as it checks at first if the value of the first character is equal or not to the value of the last character;If they are equal it wont execute the code in the next braces as here I used the condition of not-equal indicated by != .This operation will continue for the next characters; second and before last and so on.

{flag=false; 

Again, this part will be executed if the characters are not similar, and it tends to change the value of flag to false not to enter the operation of the parent if-statement.

}}else

else-statement is executed if if-statement is not executed

{break; 

if it is not a palindrome, exit the for loop if flag is true cout “Palindrome” otherwise output “Not Palindrome”

if(flag){cout<<"Palindrome";}else{cout<<"Not Palindrome";}cin.get();return 0;}
proteous_palindrom_mycomsats

Palindrome Code Simulation at Proteus Using Atmega16

So its time to test our program. If you still unfamiliar that what we are doing so here a little introduction for you.We are Making an palindrome code in assembly language. First we developed our logic then we wrote a code and we had saved this code in hex file and now we are going to test this code.So the platform we are using to test code is Proteus. If you don’t know how to use it please google about it as this is not our objective right now.Please download attached zip and extract and open “Test_bench.DSN”.You will see it like following figure.Please follow these steps to burn a program in micro controller

  • Now double click at micro controller “ATMEGA16″
  • In the front of “Program file” their is option for Browse.
  • Now select your hex file. (Hex file is attached in zip)
  • Press ok

Now at the bottom there is option for play. Click at it.

PORT ‘D’ (INPUT PORT)

We are using PORT D as input port and so we have attached logic states to each pin of PORT D for quick testing.

PORT ‘C’ (OUTPUT PORT)

If the number of inputs would be palindrome then the logic state at 5th pin of PORT C will be high or 1 and if the number of inputs would not be palindrome then the logic state at 3rd pin of PORT C will be high or 1.

PORT ‘A’ , PORT ‘B’ (Testing Ports)

We used PORT ‘A’ and PORT ‘B’ for testing of out at different stages of code that we are get output same as we made a basic concept or we did paper work.For testing at each stage you can use instruction like this

OUT PORTA, R28

 

PART2

My Turn.I tried to explain all this code as much as i can.But if you are getting any problem or tried to improve this code that would be great and please consider sharing it in below comments. Tell me my mistakes as nothing is perfect. Thanks for reading.

Waiting for you kind comments.  Download Files

Binary code

Palindrome Program Using Assembly Language Code

In this post we will write Palindrome Program Using Assembly Language Code. For basic concept for Palindrome Program please take a look at my previous post “Palindrome Program Using Assembly Language Basic Concept

Please note that the register may not be same as i used in previous post.


start:
LDI R16,0X00
LDI R17,0XFF
LDI R28,0X04 ; 0000 0100
LDI R22,0X10
ldi lsb, 0x0f ;0000 1111
ldi msb, 0xf0; 1111 0000
OUT PORTD, R17
OUT PORTB, R17
OUT PORTC, R17
OUT PORTA, R17
OUT DDRD, R16
OUT DDRB, R17 ; OUTPUT
OUT DDRC, R17 ; OUTPUT
OUT DDRA, R17 ; OUTPUT FOR TEST  UNORDERBITS
forever:
IN r30,PIND
MOV r31,r30
MOV r27,r30 ; r27 is used as unorder to compare final result
And r30,lsb
And r31,msb
MOV r29,r30
SWAP r30              ;swap
MOV r23,r30
MOV r24,r30
MOV r25,r30
MOV r26,r30
LDI R19,0X80 ; 1000 0000
AND r23,r19 ;r35 = 1000 0000
LDI r19,0X40 ; 0100 0000
AND r24,r19 ;r36 = 0100 0000
LDI r19,0X20 ; 0010 0000
AND r25,r19 ;r37 = 0010 0000
LDI r19,0X10 ;0001 0000
AND r26,r19 ;r38 = 0001 0000

; shifting Register
LSR r23  ;0100 0000
LSR r23  ;0010 0000
LSR r23  ;0001 0000
LSR r24 ;0010  0000
LSL R25   ;0100 0000
LSL R26 ; 0010 0000
LSL R26 ; 0100 0000
LSL R26 ; 1000 0000
ADD r23,r24
ADD r23,r25
ADD r23,r26
CP r23,r31
BREQ GOOD
OUT PORTC,R28
JMP END
GOOD:
OUT PORTC,R22
END:
rjmp forever

<PART1         PART3>

Download Files

palindr2

Palindrome Program Using Assembly Language Basic Concept

Today we are going to make an program which will show that number is Palindrome or not using Assembly Language for Atmega16.Here is our question.

Write a code that detects whether a number is Palindrome or not . If they are equal set the 5th bit of DDRC and PORTC registers. Otherwise set the 3rd bit of DDRC and PORTC registers. Simulate the code on Proteus.

I have divide this tutorial in three parts.

  1. Basic Concept
  2. Code + Code explanation
  3. Proteus Simulation

Basic Concept


Take Input

Lets take input from PORTD and save the result in a register R 16. So I chose input as 1111 1111.The green color is showing LSBs(Lower Nibble) and yellow is for MSBs (Higher Nibble)

Extract the Higher Nibble

We will extract higher nibble and save the results in R17

Extract the Lower Nibble

We will extract lower nibble and save the results in R18

SwapLower Nibble

Now we will swap lower nibble register R18 having LSBs. The swaping can be done simply by swap command and our register will become as in below table .Now notice that both R17 and R18 have MSBs

Separate Bits and Shift

Separate all four bits in four different registers1:- Left Shift the register three times holding the most significant bit of the four MSB’s2: Left shift 2nd MSB register having 2nd MSB.3: Right shift the third MSB register.4: Right shift three times the forth register.

Add All Registers

Now add all the register having reorder bits.R23 = R19 + R20 + R21 + R22

Compare the Reordered Bits

Now compare the reordered bits with unordered bits. For code please visit Part2

Part2

 Download Files