Monday, September 28, 2020

Write a C program to find hamming distance. For example Hamming distance d(v1 ,v2)=3 if v 1=011011,v2=110001.

Hamming distance d(v1 ,v2)=3 if v 1=011011,v2=110001.

 Example :-

 
#include<stdio.h>
#include<string.h>
int main()
{
int i,l1,l2,count=0;
char one[200],two[200];
while(1)
{
printf("Enter First String ::\n");
scanf("%s",&one);
printf("Enter Second String ::\n");
scanf("%s",&two);

l1=strlen(one);
l2=strlen(two);
if(l1!=l2)
{
printf("\nLength of both strings must be same, enter again!\n");
}
else
{
for(i=0;i<l1;i++)
{
if(one[i]!=two[i])
{
count++;
}
}
break;
}
}
printf("\nHamming distance :-\t%d",count);
return 0;
}
 

 

 OUTPUT

 

 
Enter First String ::
Coding
Enter Second String ::
Blocks

Hamming distance :- 6
 

 

 

Write a program to create Calendar in Android.

Calendar in Android.

MainActivity.java

package com.example.sunny.calendar;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private TextView thedate;
private Button btngocalendar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
thedate = (TextView) findViewById(R.id.date);
btngocalendar = (Button) findViewById(R.id.btngocalendar);

Intent incoming = getIntent();
String date = incoming.getStringExtra("date");
thedate.setText(date);

btngocalendar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Calendar.class);
startActivity(intent);
}
});
}
}

 

Calendar.java

 



package com.example.sunny.calendar;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CalendarView;

public class Calendar extends AppCompatActivity {

private static final String TAG = "Calendar";
private CalendarView mCal;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar);

mCal = (CalendarView) findViewById(R.id.calView);
mCal.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView CalendarView, int year, int month, int dayOfMonth) {
String date = year + "/" + month + "/"+ dayOfMonth ;
Log.d(TAG, "onSelectedDayChange: yyyy/mm/dd:" + date);
Intent intent = new Intent(Calendar.this,MainActivity.class);
intent.putExtra("date",date);
startActivity(intent);

}
});
}
}



 

activity_main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f5b041 "
tools:context=".MainActivity">

<TextView
android:id="@+id/cdate"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="215dp"
android:textColor=" #2e86c1 "
android:text="Date"
android:textAlignment="center"
android:textSize="25dp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.219" />

<Button
android:id="@+id/btncalendar"
android:layout_width="266dp"
android:layout_height="47dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="82dp"
android:text="Go to calendar"
tools:layout_editor_absoluteX="47dp"
tools:layout_editor_absoluteY="8dp" />

</RelativeLayout>


activity_calendar.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background=" #f8c471 "
tools:context=".Calendar">


<CalendarView
android:id="@+id/calView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>


AndroidManifest.xml

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sunny.calendar">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Calendar"></activity>
</application>

</manifest>


OUTPUT

Calendar in Android Studio
Calendar in Android Studio

Calendar in Android Studio









Monday, September 14, 2020

Write a Program to print pyramid using # in C.

 Print pyramid using # in C.

 EXAMPLE :-

 
/* This is Pattern program using # in C */

#include <stdio.h>
/* Main function starts from here*/
int main()
{
int i, j, rows;

/* This statement will numer of rows message */
printf("Enter number of rows: ");
scanf("%d",&rows);

/* using for loop*/
for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j)
{
printf("# ");
}
printf("\n");
}
return 0;
}
 

 

OUTPUT

 
Enter number of rows: 5

#
# #
# # #
# # # #
# # # # # 
 

 

 

Saturday, September 12, 2020

Write a program to create Menu List (Option Menu,Context Menu,PopUp Menu) In Android.

Menu List (Option Menu,Context Menu,PopUp Menu) In Android.

 Step :-1 Create New Project

Step : -2 Create New menu.xml file

 

MainActivity.java


package com.example.sunny.menus;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener {
private ActionMode actionMode=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


}
public void PopUpMenu(View v) {
PopupMenu popup = new PopupMenu(this, v);

popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.menu);
popup.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
private ActionMode.Callback actionModeCallback = new ActionMode.Callback() {

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}


@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.setting:
Toast.makeText(MainActivity.this,"Clicked Setting",Toast.LENGTH_SHORT).show();
mode.finish();
return true;

case R.id.logout:
Toast.makeText(MainActivity.this,"Clicked Logout",Toast.LENGTH_SHORT).show();
mode.finish();
return true;
default:
return false;
}
}

@Override
public void onDestroyActionMode(ActionMode mode) {
actionMode = null;
}
};
public void ActionMode(View view){

TextView click=findViewById(R.id.conttext_menu);
click.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View view) {
if (actionMode != null) {
return false;
}

actionMode = MainActivity.this.startActionMode(actionModeCallback);
view.setSelected(true);
return true;
}
});

menu.xml }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, "Selected Item: " +item.getTitle(), Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.search_item:
// do your code
return true;
case R.id.upload_item:
// do your code
activity_main.xml return true;
case R.id.copy_item:
// do your code
return true;
case R.id.print_item:
// do your code
return true;
case R.id.share_item:
// do your code
return true;
case R.id.bookmark_item:
// do your code
return true;
default:
return super.onOptionsItemSelected(item);
}
}

@Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(this, "Selected Item: " +item.getTitle(), Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.search_item:
// do your code
return true;
case R.id.upload_item:
// do your code
activity_main.xml return true;
case R.id.copy_item:
// do your code
return true;
case R.id.print_item:
// do your code
return true;
case R.id.share_item:
// do your code
return true;
case R.id.bookmark_item:
// do your code
return true;
default:
return super.onOptionsItemSelected(item);
}

}
}


 

activity_main.xml

 



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/menu2"
tools:context=".MainActivity">


<Button
android:id="@+id/conttext_menu"
android:layout_width="150dp"
android:layout_height="83dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="17dp"
android:layout_marginLeft="17dp"
android:layout_marginTop="233dp"
android:onClick="ActionMode"
android:background="#0080ff"
android:text="Context Menu"
android:textSize="20dp" />

<Button
android:id="@+id/popup_menu"
android:layout_width="150dp"
android:layout_height="83dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="288dp"
android:onClick="PopUpMenu"
android:background="#0080ff"
android:text="Pop Up Menu"
android:textSize="20dp" />


</RelativeLayout>



menu.xml



<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="Setting" android:id="@+id/setting">

</item>

<item android:id="@+id/logout" android:title="Logout" app:showAsAction="never"></item>

<item android:id="@+id/search_item"
android:title="Search" />
<item android:id="@+id/upload_item"
android:title="Upload" />
<item android:id="@+id/copy_item"
android:title="Copy" />
<item android:id="@+id/print_item"
android:title="Print" />
<item android:id="@+id/share_item"
android:title="Share" />
<item android:id="@+id/bookmark_item"
android:title="BookMark" />


</menu>


OUTPUT

 

                                                      Android Menu Program in  Menu


                                            Android Menu Program on Option Menu 

After Click Option Menu Show......

                                            Android Menu Program on Option Menu


                                             Android Menu Program on Context Menu

After Click Context Menu Show.........

                                              Android Menu Program on PopUp Menu


                                               Android Menu Program on PopUp Menu


                                            Android Menu Program on PopUp  Menu




Friday, September 11, 2020

Write a program to create Menu List In Android.

Android Option Menu Example

EXAMPLE :- 

MainActivity.java

package com.example.sunny.menu;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


}


public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, "Selected Item: " +item.getTitle(), Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.search_item:
// do your code
return true;
case R.id.upload_item:
// do your code
return true;
case R.id.copy_item:
// do your code
return true;
case R.id.print_item:
// do your code
return true;
case R.id.share_item:
// do your code
return true;
case R.id.bookmark_item:
// do your code
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}


activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/menu"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This Is Menu Program!"
android:textColor="@color/green"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.478" />

</android.support.constraint.ConstraintLayout>


AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sunny.menu">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


options_menu.xml


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/search_item"
android:title="Search" />
<item android:id="@+id/upload_item"
android:title="Upload" />
<item android:id="@+id/copy_item"
android:title="Copy" />
<item android:id="@+id/print_item"
android:title="Print" />
<item android:id="@+id/share_item"
android:title="Share" />
<item android:id="@+id/bookmark_item"
android:title="BookMark" />



</menu>


OUTPUT

Menu List In Android.

Menu List In Android.

Menu List In Android.





Wednesday, September 9, 2020

Write a Lex program to count the number of comment lines in a given C program.

Count the number of comment lines in a given C program using Lex

EXAMPLE :-


%{ int com=0; %} %% "/*"[^\n]+"*/" {com++;fprintf(yyout, " ");} %% int yywrap(void){} int main() { printf("Write a C program find Comment line : \n"); yyout=fopen("Output", "w"); yylex(); printf("Comment=%d\n",com); return 0; }


OUTPUT


sunny@sunny-PC:~/Desktop$ flex comment.l sunny@sunny-PC:~/Desktop$ gcc lex.yy.c sunny@sunny-PC:~/Desktop$ ./a.out
Write a C program find Comment line :
/* Write Hello World in C*/ #include <stdio.h> /* Main function starts from here*/ int main() { /* This statement will print Hello World message */ printf(“Hello World!”); return 0; }
            Ctrl+d
Comment=3












Write a Lex program which adds line numbers to the given file and display the same onto the standard output.

LEX program to add line numbers to a given file


EXAMPLE :-

Create first file line.l or line.lex , and Create Second file line.java

line.l

%{ int line_number = 1; %} line .*\n %% {line} { printf("%10d %s", line_number++, yytext); } %% int yywrap(){} int main(int argc, char*argv[]) { extern FILE *yyin; yyin = fopen("line.java","r"); yylex(); return 0; }


INPUT :-

line.java


class hello{ public static void main(String args[]){ System.out.println("Hello World !"); } }


OUTPUT


sunny@sunny-PC:~/Desktop$ flex line.l sunny@sunny-PC:~/Desktop$ gcc lex.yy.c sunny@sunny-PC:~/Desktop$ ./a.out 1 class hello 2 { 3 public static void main(String args[]) 4 { 5 System.out.println("Hello World !"); 6 }          7 }