Showing posts with label String Operations. Show all posts
Showing posts with label String Operations. Show all posts

Saturday, July 11, 2015

String,Strings Function and Operation on String

String: String is a sequence of Characters.

Strings Functions:

1. strlen();  --Returns the length of the String.
    Strlen() Function source code

    Length of "Hello World!".

    2. strpos(); --Return the position of Substring or Second string in First String else return false.

    strpos() string position function
    output of the function



    3. strrev(); --Reverse the entered String.
    Reverse the string strrev() source code

    Output of original string along with revered string


    4. str_word_count(); --Return the Worlds in String Space Separated.
    str_word_count() return the output total number of word in string.

    output of the above code.

    5. str_replace(); --Function replace the first parameter,with second from third String.
          

      stropr
      Source Code of str_replace()

      outputwindow
      output of above code.


                    
      © 2015 Rajendra Kumar Yadav Learn Computer Tricks and Programming & Learn PHP

      Saturday, June 27, 2015

      Assignment 1 Question 3 (Server side and Client Side Scripting)

      <?php
      /**
      * @author   :       Rajendra Kumar Yadav
      * @filename :     a1q3.php
      * @date     :         23:53:58 Sat 27- Jun - 2015 IST
      * College Work: Assignment 1 Question 3
      * College :           ATSS College PHP Practical
      */
       ?>
       <html Copyright (c) 2015 Copyright Rajendra Kumar Yadav All Rights Reserved. lang="en">
       <head> <title> Assignment 1 Question 3 </title> </head>
       <h3 class='test'> Assignment 1 Question 3 </h3>
       <hr align=center color=red size=2px>
       <body align=center bgcolor= antiquewhite>
         <style media="screen">
         .test {

            font-weight: normal;
            font-family: "Times new Roman";
            align-items: center;
            font-language-override: normal;
            color: inherit;
         }
         </style>
         <pre class="test">
           <form method="post" action="<?php $_PHP_SELF ?>">
             <label for="str1">Enter String 1 </label>      <input type="text" name="str1" required autofocus value="<?php if(isset($_POST['str1'])) echo $_POST['str1']; ?>"/><br/>
             <label for="str2">Enter String 2 </label>     <input type="text" name="str2" required autofocus value="<?php if(isset($_POST['str2'])) echo $_POST['str2']; ?>"/><br/>

             <h3 class='test'> --:Select Your Choice:-- </h3>
             <hr color=red size=0.5px align=center>
                    Append Strings                  <input type="radio" value="append" name="ch"/>
        Size of length of String 1                  <input type="radio" value="string1len" name="ch"/>
              Reverse First String                  <input type="radio" value="stringrev" name="ch"/>
             <hr color=red size=0.5px align=center>
             <input type="submit" value="Check"/>     <input type="reset" value="Clear Areas"/>
           </form>
         </pre>
       </body>
       </html>

       <?php
      if($_SERVER['REQUEST_METHOD']=='POST')
      {

      $str1=$_POST['str1']; // GOLABL VARIBALE FROM THE HTML PART.
      $str2=$_POST['str2']; // GOLABL VARIBALE FROM THE HTML PART.
      $ch=@$_POST['ch'];     // GOLABL VARIBALE FROM THE HTML PART.

      if($ch=='append')
      {
        echo "<hr align=center color=red> Concated String is:-"." $str1" . "$str2";
      }
       if($ch=='string1len') {
        echo "<hr align=center color=red> String length is :-  ". strlen($str1);
      }
      if($ch=='stringrev') {
        $temp=strrev($str1);
        echo "<hr align=center color=red>First String Reverse & second as its is :- ". " $temp "." $str2";
      }

      }
      ?>

      Images to Work on the page...


      concate
      Concatenation of Two String

      emptyform
      Main Form View



      Reverse
      Reverse First String and Print Overall String


      strlen
      Length of First String



      Assignment 1 Question 2 (Server Side Scripting Part using PHP).

      <?php
      /*
      @autor Rajendra Kumar Yadav
      @Check the Substring position
      @Check First and last occurance Position
      @ Replace Small Substring with some new substring.
      */

      $n=$_POST['str1'];
      $n1=$_POST['str2'];
      $n2=$_POST['str3'];
      $ch=$_POST['ch'];
      function POSITION($n,$n1)
      {
      $c=strpos($n,$n1);
      $c1=strrpos($n,$n1);
      echo "The Substring 2 Occurs at position ".$c." and ".$c1;
      }
      function OCCUR_SUBSTR($n,$n1)
      {
      $c2=substr_count($n,$n1);
      echo "The Substring ".$n1 ." occured ".$c2." times.";
      }

      if($ch=="first_last_pos")
      {
      POSITION($n,$n1);
      }
      if($ch=="occ_substring")
      {
      OCCUR_SUBSTR($n,$n1);
      }
      function REPLACER($n,$n1,$n2)
      {
      $c3=strlen($n1);
      $c4=substr_replace($n,$n2,$c,$c3);
      echo "First String is ".$n;
      echo "Second String is ".$n1;
      echo "New String is ".$n2;

      }
      if($ch=="replacer")
      {
      REPLACER($n,$n1,$n2);
      }

      ?>

      Assignment 1 Question 2 (HTML Form Part)

      <?php
      /**
      *@author Rajendra Kumar Yadav
      *@File a1q2.php (Assignment 1 Question 2).
      * Other Page name is "a1q1_server_Script.php"
      * which control the server script
      */
      ?>
      <html lang="en">
      <head> <title> Assignment 1 Question 2 </title> </head>
      <h3 align="center">  Assignment 1 Question 2 </h3>
      <hr color="lightblue" size=0.5px width=1024>
      <body align=center bgcolor=lightgrey>
      <pre>
      <div id=main>
      <form method="post" action="a1q1_server_Script.php">
      Enter the First String <input type=text name=str1><br/>
      Enter The Second String <input type=text name=str2><br/>
      Enter The Third String <input type=text name=str3><br/>
      <hr color="navy" width=800px size=0.5px align=center>
      First and last Position <input type=radio name="ch" value="first_last_pos"/>
      Occurance of substring <input type=radio name="ch" value="occ_substring"/>
      Replace the Second with Third String <input type=radio name="ch" value="replacer"/>
      <hr color="navy" width=800px size=0.5px align=center/>
      <input type=submit value=Check> <input type=reset value=Clear>
      </form>
      </div>
      </pre>
      </body>
      </html>

      Friday, June 26, 2015

      Assignment 1 Question 1

      <?php
      /*
      * @author Rajendra Kumar Yadav
      * @date 23:55:40 26-Jun-2015 IST
      * ATSS First PHP Program
      * Filename a1q1.php
      * Assignment 1 Question 1 for Semester III.
      */
      ?>
      <html lang="en">
      <head> <title> Assignment 1, Question 1! </title> </head>
      <body bgcolor='#000000'>
      <center>
      <h3> Welcome to Assignment 1 and Question 1!</h3>

      <?php
      if($_SERVER['REQUEST_METHOD']=='POST')
      {
                      $str=$_POST['str']; //Global variable
                      $ch=$_POST['ch'];           //Global variable

                      function COU($str) //Function to check the vowel occurance
                      {
                                      $cnt=0;
                                      for($i=0;$i<strlen($str);$i++)
                                      {
                                                      $copystr=$str;
                                                      $c=substr($copystr,$i,1); //main statement for this and logic are below...
                                                                      if($c=='A' || $c=='a' || $c=='E' || $c=='e' || $c=='I' || $c=='i' || $c=='O' ||$c=='o' || $c=='U' || $c=='u')
                                                                      {
                                                                                      $cnt++;
                                                                      }
                                      }
       echo "<hr color=#785be0>";
                                      echo "<center>Total Occurance of Vowel are <strong>".$cnt."</strong></center>";
                      }
                      if($ch=="count") //checking if radio is to count the vowel to call COU($str) with $str parameter
                      {
                                      COU($str);
                      }
                      function EACH_OCCUR($str)       //same other fucntion defination...
                      {
                      $acnt=$ecnt=$icnt=$ocnt=$ucnt=0;
                      for($i=0;$i<strlen($str)-1;$i++)
                      {
                                      $strcpy=$str;
                                      $c=substr($strcpy,$i,1); //statement under for loop to check each position of the string and below the logic are...
                                      {
                                                      if($c=='A' || $c=='a')
                                                                      $acnt++;
                                                      if($c=='E' || $c=='e')
                                                                      $ecnt++;
                                                      if($c=='I' || $c=='i')
                                                                      $icnt++;
                                                      if($c=='O' || $c=='o')
                                                                      $ocnt++;
                                                      if($c=='U' || $c=='u')
                                                                      $ucnt++;
                                      }
                      }  echo "<hr color=#785be0>"; //printing the occurance ...
                      echo "A occurs <strong>".$acnt."</strong> times.<br>";
                      echo "E occurs <strong>".$ecnt."</strong> times.<br>";
                      echo "I occurs <strong>".$icnt."</strong> times.<br>";
                      echo "O occurs <strong>".$ocnt."</strong> times.<br>";
                      echo "U occurs <strong>".$ucnt."</strong> times.<br>";
      }
                      if($ch=="occ")
                      {
                                                      EACH_OCCUR($str); //if radio to count each vowel seprately soo call to EACH_OCCUR($str) with parameter $str.
                      }
                      function PAL($str)  //for palindrom
                      {
                                      $strcpy=strrev($str); //reverse the string and store in the $strcpy
                                       echo "<hr color=#785be0>";
                                      if($strcpy==$str)
                                                      print("Entered String is Palndorm");
                                      else
                                                      print("Entered string is not a Palndrom");
                      }
                      if($ch=="pal")
                      {
                                      PAL($str);
                      }
      }
      ?>

      <hr color="#000000">
      <form method="post">
      <label for="str">Enter The String</label>             <input title="text" name="str" value="<?php if(isset($_POST['str'])) echo $_POST['str'];?>" required/><br/>
      <br><br><hr color="#000000">
      <label for="Choice"> Select your Choice </label><hr color="#201100" width="180px" align="center">
      <br><br>
      Count the number of Vowel                                                                       <input type="radio" name="ch" value="count" required>
      Occurence of Each Vowel                                                                                             <input type="radio" name="ch" value="occ">
      To check that the String is Palindrom of not         <input type="radio" name="ch" value="pal"><br/>
      <hr color="black">
      <br><br>
      <input type="submit" value="Check">                                  <input type="reset" value="Clear">
      </form>
      </center>
      </body>

      </html>