PHP – Split sentence into words


Q.) Ignore ‘Spaces’ and split sentence into words and store into array

Function:

<?php
function splitIt($s){
			$arr = array('.',',');
			$s = str_replace($arr," ",$s);
			$s = preg_replace("/( )+/", " ", $s);
			$s = preg_replace("/^( )+/", "", $s);
			$s = preg_replace("/( )+$/", "", $s);
			$tokens = explode(" ",$s);
			return $tokens;
		}
?>

Input:

 <?php
 $answer = splitIt("   hello world. prt this sentence,please  ");
 print_r($answer);
?>

Output:

 Array ( 
    [0] => hello
    [0] => world
    [0] => prt
    [0] => this
    [0] => sentence
    [0] => please
)

The number which is prime and also palindrome.


After many days of gap…  back to blogging…….

Q.) An integer is said to be a palindrome if it is equal to its reverse. For example, 79197 and 324423 are palindromes. In this task you will be given an integer N, 1 ≤ N ≤ 1000000. You must find the smallest integer M ≥ N such that M is a prime number and M is a palindrome. For example, if N is 31 then the answer is 101.

Input format
A single integer N, (1 ≤ N ≤ 1000000), on a single line.
Output format
Your output must consist of a single integer, the smallest prime palindrome greater than or equal to N

Sol.)

#include<stdio.h>
void main()
{
   long int a;
   printf("Enter number: ");
   scanf("%li",&a);
   if(a <= 1000000)
    while(1)
    {
     if(palindrome(a)){
     if(prime(a)) {printf("Number is: %d",a);getch();return;}
    }
  a=a+1;
}
else printf("Plz enter number less than 1000000 next time");
getch();
}
int prime(long int a){
long int i;
for(i=2;i<=a/2;i++) if(a%i==0)return 0;

return 1;
}

int palindrome(long int a){
  long int temp=a,reverse=0;
 while( temp != 0 ){
  reverse = reverse * 10;
  reverse = reverse + temp%10;
  temp = temp/10;
 }
  if(reverse == a) return 1;
  else return 0;
}

Shivaji varma… 😀