About
// To find count of each character in a string
void printfrequencyofcharacters(char *s) { int i,j,count=0,n;
n=stringlength(s);
printf(" frequency count character in string:\n");
for(i=0;i<n;i++)
{
count=1;
if(s[i])
{
for(j=i+1;j<n;j++)
{
if(s[i]==s[j])
{
count++;
s[j]='\0';
}
}
printf(" '%c' = %d \n",s[i],count);
}
}
// Palindrome
Demonstrate UART and LCD interface using LPC2148 microcontroller. a. Display character in LCD, using UART (Tx) b. Display character in LCD. Character received from your PC keyword and send to LCD using UART (RX).
*/
include <lpc214x.h>
include <string.h>//Includes LPC2148 register definitions
define PLOCK 0x00000400 // for PLL
define THRE (1<<5) // Transmit Holding Register Empty
define RDR (1<<0) // Receiver Data Ready
define MULVAL 15
define DIVADDVAL 1
define NEW_LINE 0xA // Character for new line .. analogus to '\n'
define ENTER 0xD // Ascii code for Enter
void initUART0(void); void U0Write(char data); char U0Read(void); void initClocks(void);
void setupPLL0(void); void feedSeq(void); void connectPLL0(void);
define DATA_PORT() IO0SET=(1<<16) //Function to select data port on LCD
define READ_DATA() IO0SET=(1<<17) //Function to select read operation on LCD
define EN_HI() IO0SET=(1<<18) //Function to Enable LCD
define COMMAND_PORT() IO0CLR=(1<<16) //Function to select command port on LCD
define WRITE_DATA() IO0CLR=(1<<17) //Function to select write operation on LCD
define EN_LOW() IO0CLR=(1<<18) //Function to disable LCD
void Delay(unsigned char j)
{
unsigned int i;
for(;j>0;j--)
{
for(i=0; i<60000; i++);
}
}
void Delay_Small(unsigned char j) { unsigned int i; for(;j>0;j--) { for(i=0; i<1000; i++); } }
unsigned char Busy_Wait() //This function checks the busy status of LCD { unsigned int temp=0; EN_LOW(); COMMAND_PORT(); READ_DATA();
IO0PIN&=0xFF87FFFF;
IO0DIR&=0xFF87FFFF;
IO0PIN|=0x00400000;
do{ EN_HI(); EN_LOW(); EN_HI(); EN_LOW(); temp=IO0PIN; } while((temp & 0x00400000)==0x00400000); EN_LOW(); WRITE_DATA(); IO0DIR&=0xFF80FFFF; IO0DIR|=0x007F0000; return (0); }
void LCD_Command(unsigned int data) //This function is used to send LCD commands { unsigned int temp=0; EN_LOW(); COMMAND_PORT(); WRITE_DATA();
temp=data; //IO0PIN&=0xFF87FFFF; IO0PIN&=0x00010000; IO0PIN|=(temp & 0xF0) << 15;
EN_HI(); EN_LOW();
temp=data & 0x0F; //IO0PIN&=0xFF87FFFF; IO0PIN&=0x00010000; IO0PIN|=(temp) << 19;
EN_HI(); EN_LOW(); //while(Busy_Wait()); Delay(10); }
void LCD_Data(unsigned int data) //This function is used to send data to LCD { unsigned int temp=0; EN_LOW(); DATA_PORT(); WRITE_DATA();
temp=data; //IO0PIN&=0xFF87FFFF; IO0PIN&=0x00010000; IO0PIN|=(temp & 0xF0) << 15;
EN_HI(); EN_LOW();
temp=data & 0x0F;
//IO0PIN&=0xFF87FFFF; IO0PIN&=0x00870000; IO0PIN|=(temp) << 19;
EN_HI(); EN_LOW(); Delay_Small(1); }
void LCD_Init()
{
LCD_Command(0x02);
LCD_Command(0x28);
LCD_Command(0x0C);
LCD_Command(0x06);
}
void LCD_String(unsigned char data) { while(data) { LCD_Data(*data); data++; } }
unsigned char str[32]=" "; unsigned char st1[32]=" "; unsigned char st2[32]=" ";
int i=0;
void palind(unsigned char *str) { int l = 0; int h = strlen(str) - 1; while (h > l) { if (str[l++] != str[h--]) { strcpy(st1,"NOT PALINDROME"); LCD_Init(); LCD_Command(0x01); Delay(20);
LCD_Command(0x80);
LCD_String(&st1[0]);
LCD_Command(0xC0);
LCD_String(&st1[16]);
}
}
strcpy(st2,"PALINDROME");
LCD_Init();
LCD_Command(0x01);
Delay(20);
LCD_Command(0x80);
LCD_String(&st2[0]);
LCD_Command(0xC0);
LCD_String(&st2[16]);
} int main(void) { PINSEL0 = 0x00000000; // Enable GPIO on all pins PINSEL1 = 0x00000000; PINSEL2 = 0x00000000;
Delay(20);
IO0DIR = (1<<22) | (1<<21) | (1<<20) | (1<<19) | (1<<18) | (1<<17) | (1<<16); // Set P0.16, P0.17, P0.18, P0.19, P0.20, P0.21, P0.22 as Output
initClocks(); // Set CCLK=60Mhz and PCLK=60Mhz
initUART0();
while(1)
{
char c = U0Read(); // Read Data from Rx
if( c == ENTER ) // Check if user pressed Enter key
{
U0Write(c); // Send New Line ASCII code change line
break;
}
else
{
str[i]=c;
i++;
U0Write(c); // Write it to Tx to send it back
}
}
LCD_Init();
LCD_Command(0x01);
Delay(20);
LCD_Command(0x80);
LCD_String(&str[0]);
LCD_Command(0xC0);
LCD_String(&str[16]);
palind(&str[0]);
return 0;
}
void initUART0(void)
{
PINSEL0 = 0x5; / Select TxD for P0.0 and RxD for P0.1 /
U0LCR = 3 | (1<<7) ; / 8 bits, no Parity, 1 Stop bit | DLAB set to 1 /
U0DLL = 110;
U0DLM = 1;
U0FDR = (MULVAL<<4) | DIVADDVAL; / MULVAL=15(bits - 7:4) , DIVADDVAL=0(bits - 3:0) /
U0LCR &= 0x0F; // Set DLAB=0 to lock MULVAL and DIVADDVAL
//BaudRate is now ~9600 and we are ready for UART communication!
}
void U0Write(char data) { while ( !(U0LSR & THRE ) ); // wait till the THR is empty // now we can write to the Tx FIFO U0THR = data; }
char U0Read(void) { while( !(U0LSR & RDR ) ); // wait till any data arrives into Rx FIFO return U0RBR; }
void initClocks(void) { setupPLL0(); feedSeq(); //sequence for locking PLL to desired freq. connectPLL0(); feedSeq(); //sequence for connecting the PLL as system clock
//SysClock is now ticking @ 60Mhz!
VPBDIV = 0x01; // PCLK is same as CCLK i.e 60Mhz
//Using PLL settings as shown in : http://www.ocfreaks.com/lpc214x-pll-tutorial-for-cpu-and-peripheral-clock/
//PLL0 Now configured!
}
//---------PLL Related Functions :---------------
void setupPLL0(void) { //Note : Assuming 12Mhz Xtal is connected to LPC2148.
PLL0CON = 0x01; // PPLE=1 & PPLC=0 so it will be enabled
// but not connected after FEED sequence
PLL0CFG = 0x24; // set the multipler to 5 (i.e actually 4)
// i.e 12x5 = 60 Mhz (M - 1 = 4)!!!
// Set P=2 since we want FCCO in range!!!
// So , Assign PSEL =01 in PLL0CFG as per the table.
}
void feedSeq(void) { PLL0FEED = 0xAA; PLL0FEED = 0x55; }
void connectPLL0(void) { // check whether PLL has locked on to the desired freq by reading the lock bit // in the PPL0STAT register
while( !( PLL0STAT & PLOCK ));
// now enable(again) and connect
PLL0CON = 0x03;
}
To find duplicates in a string
char str[64]; char words[6][16];
int i = 0;
int j = 0;
int k = 0;
int l = 0;
printf("Enter string: ");
scanf("%[^\n]s", str);
while (str[i] != 0) {
if (str[i] == ' ') {
words[k][j] = '\0';
k++;
j = 0;
}
else {
words[k][j] = str[i];
j++;
}
i++;
}
words[k][j] = '\0';
j = 0;
for (i = 0; i < k; i++) {
int present = 0;
for (l = 1; l < k + 1; l++) {
if (words[l][j] == '\0' || l == i)
continue;
if (strcmp(words[i], words[l]) == 0) {
words[l][j] = '\0';
present = present + 1;
}
}
}
j = 0;
printf("Result is:\n");
for (i = 0; i < k + 1; i++) {
if (words[i][j] == 0)
continue;
else
printf("%s ", words[i]);
}
printf("\n");
Pass matching
include <lpc214x.h>
include <string.h>
define PLOCK 0x00000400 // for PLL
define THRE (1<<5) // Transmit Holding Register Empty
define RDR (1<<0) // Receiver Data Ready
define MULVAL 15
define DIVADDVAL 1
define NEW_LINE 0xA // Character for new line .. analogus to '\n'
define ENTER 0xD // Ascii code for Enter
define LED1_ON() IO1SET=(1<<16)
define LED2_ON() IO1SET=(1<<17)
define LED3_ON() IO1SET=(1<<18)
define LED4_ON() IO1SET=(1<<19)
define LED1_OFF() IO1CLR=(1<<16)
define LED2_OFF() IO1CLR=(1<<17)
define LED3_OFF() IO1CLR=(1<<18)
define LED4_OFF() IO1CLR=(1<<19)
define BUZZER_OFF() IO0SET=(1<<11)
define BUZZER_ON() IO0CLR=(1<<11)
void initUART0(void); void U0Write(char data); char U0Read(void); void initClocks(void);
void setupPLL0(void); void feedSeq(void); void connectPLL0(void);
void Delay(unsigned int j){
unsigned int i;
for(;j>0;j--)
{
for(i=0; i<60000; i++);
}
}
char pass[]="embedded"; int n=8; int i=0; int main(void) { PINSEL1 = 0x00000000; PINSEL2 = 0x00000000;
IO1DIR = (1<<19) | (1<<18) | (1<<17) | (1<<16); IO0DIR = (1<<11); initClocks(); // Set CCLK=60Mhz and PCLK=60Mhz initUART0(); LED1_OFF(); BUZZER_OFF();
while(1)
{
char c = U0Read(); // Read Data from Rx
U0Write(c);
//U0Write(pass[i]);
//U0Write(NEW_LINE);
if (i>=n-1){
LED1_ON();
break;
}
if( c == '\n' ) // Check if user pressed Enter key
{
i=0;
LED1_OFF();
BUZZER_OFF();
}
else if (c == pass[i]){
i++;
}
else{
BUZZER_ON();
}
}
return 0;
}
void initUART0(void) { PINSEL0 = 0x5; / Select TxD for P0.0 and RxD for P0.1 /
U0LCR = 3 | (1<<7) ; /* 8 bits, no Parity, 1 Stop bit | DLAB set to 1 */
U0DLL = 110;
U0DLM = 1;
U0FDR = (MULVAL<<4) | DIVADDVAL; /* MULVAL=15(bits - 7:4) , DIVADDVAL=0(bits - 3:0) */
U0LCR &= 0x0F; // Set DLAB=0 to lock MULVAL and DIVADDVAL
//BaudRate is now ~9600 and we are ready for UART communication!
}
void U0Write(char data) { while ( !(U0LSR & THRE ) ); // wait till the THR is empty // now we can write to the Tx FIFO U0THR = data; }
char U0Read(void) { while( !(U0LSR & RDR ) ); // wait till any data arrives into Rx FIFO return U0RBR; }
void initClocks(void) { setupPLL0(); feedSeq(); //sequence for locking PLL to desired freq. connectPLL0(); feedSeq(); //sequence for connecting the PLL as system clock
//SysClock is now ticking @ 60Mhz!
VPBDIV = 0x01; // PCLK is same as CCLK i.e 60Mhz
//Using PLL settings as shown in : http://www.ocfreaks.com/lpc214x-pll-tutorial-for-cpu-and-peripheral-clock/
//PLL0 Now configured!
}
//---------PLL Related Functions :---------------
void setupPLL0(void) { //Note : Assuming 12Mhz Xtal is connected to LPC2148.
PLL0CON = 0x01; // PPLE=1 & PPLC=0 so it will be enabled
// but not connected after FEED sequence
PLL0CFG = 0x24; // set the multipler to 5 (i.e actually 4)
// i.e 12x5 = 60 Mhz (M - 1 = 4)!!!
// Set P=2 since we want FCCO in range!!!
// So , Assign PSEL =01 in PLL0CFG as per the table.
}
void feedSeq(void) { PLL0FEED = 0xAA; PLL0FEED = 0x55; }
void connectPLL0(void) { // check whether PLL has locked on to the desired freq by reading the lock bit // in the PPL0STAT register
while( !( PLL0STAT & PLOCK ));
// now enable(again) and connect
PLL0CON = 0x03;
}
Character Read
include <lpc214x.h> //Includes LPC2148 register definitions
define DATA_PORT() IO0SET=(1<<16) //Function to select data port on LCD
define READ_DATA() IO0SET=(1<<17) //Function to select read operation on LCD
define EN_HI() IO0SET=(1<<18) //Function to Enable LCD
define COMMAND_PORT() IO0CLR=(1<<16) //Function to select command port on LCD
define WRITE_DATA() IO0CLR=(1<<17) //Function to select write operation on LCD
define EN_LOW() IO0CLR=(1<<18) //Function to disable LCD
define PLOCK 0x00000400 // for PLL
define THRE (1<<5) // Transmit Holding Register Empty
define RDR (1<<0) // Receiver Data Ready
define MULVAL 15
define DIVADDVAL 1
define NEW_LINE 0xA // Character for new line .. analogus to '\n'
define ENTER 0xD // Ascii code for Enter
void initUART0(void); void U0Write(char data); char U0Read(void); void initClocks(void);
void setupPLL0(void); void feedSeq(void); void connectPLL0(void);
unsigned char String1[16]={" UJD "}; unsigned char String2[16]={" CSE-B "};
void Delay(unsigned char j)
{
unsigned int i;
for(;j>0;j--)
{
for(i=0; i<60000; i++);
}
}
void Delay_Small(unsigned char j) { unsigned int i; for(;j>0;j--) { for(i=0; i<1000; i++); } }
unsigned char Busy_Wait() //This function checks the busy status of LCD { unsigned int temp=0; EN_LOW(); COMMAND_PORT(); READ_DATA();
IO0PIN&=0xFF87FFFF;
IO0DIR&=0xFF87FFFF;
IO0PIN|=0x00400000;
do{ EN_HI(); EN_LOW(); EN_HI(); EN_LOW(); temp=IO0PIN; } while((temp & 0x00400000)==0x00400000); EN_LOW(); WRITE_DATA(); IO0DIR&=0xFF80FFFF; IO0DIR|=0x007F0000; return (0); }
void LCD_Command(unsigned int data) //This function is used to send LCD commands { unsigned int temp=0; EN_LOW(); COMMAND_PORT(); WRITE_DATA();
temp=data; IO0PIN&=0xFF87FFFF; IO0PIN|=(temp & 0xF0) << 15;
EN_HI(); EN_LOW();
temp=data & 0x0F; IO0PIN&=0xFF87FFFF; IO0PIN|=(temp) << 19;
EN_HI(); EN_LOW(); while(Busy_Wait()); Delay(10); }
void LCD_Data(unsigned int data) //This function is used to send data to LCD { unsigned int temp=0; EN_LOW(); DATA_PORT(); WRITE_DATA();
temp=data; IO0PIN&=0xFF87FFFF; IO0PIN|=(temp & 0xF0) << 15;
EN_HI(); EN_LOW();
temp=data & 0x0F;
IO0PIN&=0xFF87FFFF; IO0PIN|=(temp) << 19;
EN_HI(); EN_LOW(); Delay_Small(1); }
void LCD_Init() { LCD_Command(0x20); LCD_Command(0x28); LCD_Command(0x0C); LCD_Command(0x06); }
void LCD_String(unsigned char data) { while(data) { LCD_Data(*data); data++; } }
unsigned char z[10]; unsigned int i=10; unsigned int y=0; int main(void) {
PINSEL0 = 0x00000000; // Enable GPIO on all pins PINSEL1 = 0x00000000; PINSEL2 = 0x00000000;
Delay(20); IO0DIR = (1<<22) | (1<<21) | (1<<20) | (1<<19) | (1<<18) | (1<<17) | (1<<16); // Set P0.16, P0.17, P0.18, P0.19, P0.20, P0.21, P0.22 as Output IO1DIR = (1<<16);
initClocks(); // Set CCLK=60Mhz and PCLK=60Mhz initUART0();
LCD_Init(); LCD_Command(0x01); Delay(20);
//LCD_Command(0x80); //LCD_String(&String1[0]); //LCD_Command(0xC0); //LCD_String(&String2[0]);
while(y<i)
{ unsigned char c = U0Read(); // Read Data from Rx U0Write(c); z[y]=c; y+=1; //U0Write(pass[i]); //U0Write(NEW_LINE); } LCD_Command(0x01); LCD_String(z); }
void initUART0(void)
{
PINSEL0 = 0x5; / Select TxD for P0.0 and RxD for P0.1 /
U0LCR = 3 | (1<<7) ; / 8 bits, no Parity, 1 Stop bit | DLAB set to 1 /
U0DLL = 110;
U0DLM = 1;
U0FDR = (MULVAL<<4) | DIVADDVAL; / MULVAL=15(bits - 7:4) , DIVADDVAL=0(bits - 3:0) /
U0LCR &= 0x0F; // Set DLAB=0 to lock MULVAL and DIVADDVAL
//BaudRate is now ~9600 and we are ready for UART communication!
}
void U0Write(char data) { while ( !(U0LSR & THRE ) ); // wait till the THR is empty // now we can write to the Tx FIFO U0THR = data; }
char U0Read(void) { while( !(U0LSR & RDR ) ); // wait till any data arrives into Rx FIFO return U0RBR; }
void initClocks(void) { setupPLL0(); feedSeq(); //sequence for locking PLL to desired freq. connectPLL0(); feedSeq(); //sequence for connecting the PLL as system clock
//SysClock is now ticking @ 60Mhz!
VPBDIV = 0x01; // PCLK is same as CCLK i.e 60Mhz
//Using PLL settings as shown in : http://www.ocfreaks.com/lpc214x-pll-tutorial-for-cpu-and-peripheral-clock/
//PLL0 Now configured!
}
//---------PLL Related Functions :---------------
void setupPLL0(void) { //Note : Assuming 12Mhz Xtal is connected to LPC2148.
PLL0CON = 0x01; // PPLE=1 & PPLC=0 so it will be enabled
// but not connected after FEED sequence
PLL0CFG = 0x24; // set the multipler to 5 (i.e actually 4)
// i.e 12x5 = 60 Mhz (M - 1 = 4)!!!
// Set P=2 since we want FCCO in range!!!
// So , Assign PSEL =01 in PLL0CFG as per the table.
}
void feedSeq(void) { PLL0FEED = 0xAA; PLL0FEED = 0x55; }
void connectPLL0(void) { // check whether PLL has locked on to the desired freq by reading the lock bit // in the PPL0STAT register
while( !( PLL0STAT & PLOCK ));
// now enable(again) and connect
PLL0CON = 0x03;
}
void printCharWithFreq(string str) {
// size of the string 'str'
int n = str.size();
// 'freq[]' implemented as hash table
int freq[SIZE];
// initialize all elements of freq[] to 0
memset(freq, 0, sizeof(freq));
// accumulate frequency of each character in 'str'
for (int i = 0; i < n; i++)
freq[str[i] - 'a']++;
// traverse 'str' from left to right
for (int i = 0; i < n; i++) {
// if frequency of character str[i] is not
// equal to 0
if (freq[str[i] - 'a'] != 0) {
// print the character along with its
// frequency
cout << str[i] << freq[str[i] - 'a'] << " ";
// update frequency of str[i] to 0 so
// that the same character is not printed
enter code here
————————————
include <bits/stdc++.h>
define ASCII_SIZE 256
using namespace std;
char getMaxOccurringChar(char* str) {
// Create array to keep the count of individual
// characters and initialize the array as 0
int count[ASCII_SIZE] = { 0 };
// Construct character count array from the input
// string.
int len = strlen(str);
int max = 0; // Initialize max count
char result; // Initialize result
// Traversing through the string and maintaining
// the count of each character
for (int i = 0; i < len; i++) {
count[str[i]]++;
if (max < count[str[i]]) {
max = count[str[i]];
result = str[i];
}
}
return result;
}
// Driver program to test the above function
int main() {
char str[] = "sample string";
cout << "Max occurring character is "
<< getMaxOccurringChar(str);
} ——————————
Max and Min occurring character in a string
include <stdio.h>
include <string.h>
int main()
{
char string[] = "grass is greener on the other side";
int i, j, min, max, length = strlen(string);
char minChar = string[0], maxChar = string[0];
int freq[length];
//Count each word in given string and store in array freq
for(i = 0; i < length; i++) {
freq[i] = 1;
for(j = i+1; j < length; j++) {
if(string[i] == string[j] && string[i] != ' ' && string[i] != '0') {
freq[i]++;
//Set string[j] to 0 to avoid printing visited character
string[j] = '0';
}
}
}
//Determine minimum and maximum occurring characters
min = max = freq[0];
for(i = 0; i < length; i++) {
//If min is greater than frequency of a character
//then, store frequency in min and corresponding character in minChar
if(min > freq[i] && freq[i] != '0') {
min = freq[i];
minChar = string[i];
}
//If max is less than frequency of a character
//then, store frequency in max and corresponding character in maxChar
if(max < freq[i]) {
max = freq[i];
maxChar = string[i];
}
}
printf("Minimum occurring character: %c\n", minChar);
printf("Maximum occurring character: %c", maxChar);
return 0;
}
—————————————————————————
Len of string
include <stdio.h>
include<string.h>
int main() { char string1[]={"naman"}; int i=0, length; while(string1[i] !='\0') { i++; } length=i; printf(" string length is %d",length); return 0; }
set2
class Node: def __init__(self, data): self.data = data self.left = None self.right = None
def construct_bst(arr): root = None for data in arr: root = insert(root, data) return root
def insert(root, data): if root is None: return Node(data) if data < root.data: root.left = insert(root.left, data) elif data > root.data: root.right = insert(root.right, data) return root
def print_in_order(root): if root is not None: print_in_order(root.left) print(root.data) print_in_order(root.right)
def find_common_ancestor(root, node1, node2): if root is None: return None if root.data > node1 and root.data > node2: return find_common_ancestor(root.left, node1, node2) if root.data < node1 and root.data < node2: return find_common_ancestor(root.right, node1, node2) return root.data
def find_lowest_common_ancestor(root, node1, node2): if root is None: return None if root.data > node1 and root.data > node2: return find_lowest_common_ancestor(root.left, node1, node2) if root.data < node1 and root.data < node2: return find_lowest_common_ancestor(root.right, node1, node2) return root.data
Main function
def main(): operations = input("Enter operations: ").split()
if operations[0] == 'A':
n = int(input("Enter the size of the input data: "))
arr = [int(input()) for _ in range(n)]
root = construct_bst(arr)
if operations[1] == 'B':
print("Output:")
print_in_order(root)
elif operations[1] == 'C':
node1 = int(input("Enter first node: "))
node2 = int(input("Enter second node: "))
print("Output:")
ancestor = find_common_ancestor(root, node1, node2)
print(ancestor)
elif operations[1] == 'D':
node1 = int(input("Enter first node: "))
node2 = int(input("Enter second node: "))
print("Output:")
ancestor = find_lowest_common_ancestor(root, node1, node2)
print(ancestor)
Execute the main function
if __name__ == '__main__': main()
—————————————————————————