About
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");
duplicates 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
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]);
}
else{
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;
}
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;
}
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;
}
//////////////////////////////////////// frequency of string
include <stdio.h>
include <string.h>
int main()
{
char string[] = "prasanth";
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;
}