PHP code for String Tokenize
<?php
$string = "Hello Kamal. How are you Doing?";
$token = strtok($string, " ");
while ($token !== false)
{
echo "$token<br>";
$token = strtok(" ");
}
?>
Learn Android very Easily and Step by Step with this blog.
ππ»π Romantic DP for Whatsapp π₯°π
PHP code for String Tokenize
<?php
$string = "Hello Kamal. How are you Doing?";
$token = strtok($string, " ");
while ($token !== false)
{
echo "$token<br>";
$token = strtok(" ");
}
?>
What is Anagram : An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once
#include <iostream>
#include <string.h>
using namespace std;
void checkAnagrams(char str1[],char str2[])
{
int hash[26];
int i;
for( i=0;i<26;i++)
hash[i]=0;
for(i=0;i<strlen(str1);i++)
{
hash[str1[i]-97]++;
}
for(i=0;i<strlen(str2);i++)
{
hash[str2[i]-97]--;
}
for(i=0;i<26;i++)
{
if(hash[i]!=0)
break;
}
if(i==26)
cout<<"Strings are anagrams";
else
cout<<"Strings are not anagrams";
}
int main(int argc, char *argv[])
{
checkAnagrams("madam curie","radium came");
return EXIT_SUCCESS;
}
Rat in Maze problem : This is a famous problem solving coding question. There is a grid filled with 1 and 0, There is a rat at position 0,0. He has to reach at the last point of Grid and the Rat can move to block having value 1.
Print the path the will take to reach.
#include <iostream>
using namespace std;
#define N 5
int path[N][N]={1,1,1,1,1,
0,0,1,0,1,
1,1,1,1,1,
1,0,1,0,0,
1,1,1,1,1
};
int sol[N][N];
bool isValid(int row,int col)
{
if(row>=0&&col>=0&&row<N&&col<N&&path[row][col]==1&&sol[row][col]==0)
return true;
else
return false;
}
bool solveRatInMaze(int row,int col)
{
if(row==N-1&&col==N-1)
{
sol[row][col]=1;
return true;
}
// int i=row,j= col;
if(isValid(row,col)==true)
{
sol[row][col]=1;
// move right
if(solveRatInMaze(row+1,col)==true)
return true;
if(solveRatInMaze(row,col+1)==true)
return true;
if(solveRatInMaze(row+1,col)==true)
return true;
if(solveRatInMaze(row-1,col)==true)
return true;
if(solveRatInMaze(row,col-1)==true)
return true;
sol[row][col]=0;
}
else
return false;
return false;
}
int main(int argc, char *argv[])
{
int i,j;
for( i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
sol[i][j]=0;
}
}
if(solveRatInMaze(0,0)==true)
{
for( i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
printf("%d ",sol[i][j]);
}
printf("\n");
}
}
else
printf("There is no path");
return EXIT_SUCCESS;
}
Output :
1 1 1 0 0
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
0 0 1 1 1
#include <iostream>
#include <math.h>
using namespace std;
int add(int a,int b)
{
int num=0,carry=0,p=0;
int abit,bbit,numbit;
while(a!=0||b!=0)
{
abit=a&1;
bbit=b&1;
numbit=abit^bbit^carry;
carry=(abit&bbit)||(abit&carry)||(bbit&carry);
num=num+numbit*(int)pow(2,p);
p++;
a=a>>1;
b=b>>1;
}
if(carry==1)
num=num+(int)pow(2,p);
return num;
}
int main(int argc, char *argv[])
{
printf("%d",add(70,40));
return 0;
}
Output :
110
C++ Code
int add1(int num)
{
int mask=1;
if(num>=0)// if number is +ve
{
while(true)
{
if(num&mask)
{
num=num^mask;
}
else
{
num=num^mask;
break;
}
mask=mask<<1;
}
return num;
}
else // if number bis -ve
{
num=abs(num);
while(true)
{
if(num&mask)
{
num=num^mask;
break;
}
else
{
num=num^mask;
}
mask=mask<<1;
}
return (-num);
}
}
int main(int argc, char *argv[])
{
cout<<add1(12563);
return EXIT_SUCCESS;
}
C++ Code
#include <iostream>
using namespace std;
void arrange(int a[],int n)
{
int i=0,zero=-1,one=-1;
while(i<n&&zero<n&&one<n)
{
if(i%2!=0) // if odd place
{
if(a[i]!=0)
{
if(zero==-1)
zero=i+1;
while(a[zero]!=0&&zero<n)
zero++;
if(a[zero]==0)
{
a[zero]=1;
a[i]=0;
zero++;
}
}
}
else
{
if(a[i]!=1)
{
if(one==-1)
one=i+1;
while(a[one]!=1&&one<n)
one++;
if(a[one]==1)
{
a[one]=0;
a[i]=1;
one++;
}
}
}
i++;
}
}
int main(int argc, char *argv[])
{
int a[]={1,0,1,1,0,0,1,1,1,0};
int n=10;
arrange(a,n);
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
return EXIT_SUCCESS;
}
C ++ Code
#include <iostream>
using namespace std;
int find(int a[],int n)
{
int low=0,up=n;
int mid;
while(low<=up)
{
mid=(up+low)/2;
if(a[mid]<a[mid-1]&&a[mid]<a[mid+1])
return a[mid];
else if(a[mid]>a[low])
low=mid+1;
else
up=mid-1;
}
return 0;
}
int main()
{
int a[]={14,16,18,2,3,4,5,6};
int num=find(a,7);
printf("%d",num);
return 0;
}
Code :
public class Test {
public static void main(String a[])
{
GregorianCalendar gc = new GregorianCalendar();
System.out.print(gc.getTime());
}
}
Output :
Fri Sep 30 10:38:51 IST 2022
<?php // Initializing a variable with filename $file = 'myfilename.jpg' ; // Extracting only filename using constants $fileWithoutExt = pathinfo ( $file , PATHINFO_FILENAME); // Printing the result echo $ fileWithoutExt; ?>
Output
|
In Android we can get the MAC Address of a Device
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// only for gingerbread and newer versions
macAddressOfDevice = getMacAddr();
Log.i("KAMLESH", "MAC Address : " + macAddressOfDevice);
}
else {
WifiManager wifiMan = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiMan.getConnectionInfo();
macAddressOfDevice = wifiInf.getMacAddress();
Log.i("KAMLESH", "MAC Address : " + macAddressOfDevice);
}
public String getMacAddr() {
try {
List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nif : all) {
if (!nif.getName().equalsIgnoreCase("wlan0")) continue;
byte[] macBytes = nif.getHardwareAddress();
if (macBytes == null) {
return "";
}
StringBuilder res1 = new StringBuilder();
for (byte b : macBytes) {
res1.append(String.format("%02X:",b));
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
return res1.toString();
}
} catch (Exception ex) {
}
return "02:00:00:00:00:00";
}
In Android we can check whether the Device is Connected to Internet or Not.
If Connected to Internet, we can check the type of connection like Mobile Data, WiFi etc
Add following permission in Manifest file
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
public boolean isInternetConnected() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
boolean isConncted = activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
return isConncted;
}
public boolean isConnectedWifi(){
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
}
public boolean isConnectedMobile(){
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
}
import java.io.*;
public class RenameFileNames
{
public static void main(String[] args)
{
String absolutePath = "E:\\Wllpapers\\unbelive";
File dir = new File(absolutePath);
File[] filesInDir = dir.listFiles();
int i = 0;
for(File file:filesInDir) {
i++;
String name = file.getName();
String newName = "pic" + i + ".jpeg";
String newPath = absolutePath + "\\" + newName;
file.renameTo(new File(newPath));
System.out.println(name + " changed to " + newName);
}
}
}
public boolean isInternetConnected(Context context) {
int result = 0; // Returns connection type. 0: none; 1: mobile data; 2: wifi
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (cm != null) {
NetworkCapabilities capabilities = cm.getNetworkCapabilities(cm.getActiveNetwork());
if (capabilities != null) {
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
return true;
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
return true;
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
return true;
}
}
}
} else {
if (cm != null) {
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) {
// connected to the internet
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
return true;
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_VPN) {
return true;
}
}
}
}
return false;
}
void showSnackBarWithMessage(String message)
{
final Snackbar snack = Snackbar.make(yourView, message, 3000);
snack.getView().setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimaryLight));
snack.setAction("OK", new View.OnClickListener() {
@Override
public void onClick(View view) {
snack.dismiss();
}
});
snack.show();
}
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
public boolean checkGPSGPSEnabled() { LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); boolean isGPSenabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); Log.i("KAMLESH","Location Enabled "+isGPSenabled); return isGPSenabled; }