PHP Programming Basics-String Operations

0
135

PHP string operations

PROBLEM DEFINITION

 Write a program to implement string concatenation, string length, string position, and string replace.

 ALGORITHM

Step 1: Start

Step 2: Enter the strings.

Step 3: Read it to string1 and string2.

Step 4: Perform concatenation using “string1.string2”

Step 5: Find sring length using strlen()

Step 6: Find string position using strpos().

Step 7: Find string replace using strreplace().

Step 8: End.

PROGRAM DEVELOPMENT

 <?php

echo “enter the choicen”;

echo “1.string concatenationn2.string lengthn3.string positionn4.string replace”;

$st1=fopen(“php://stdin”,”r”);

$n=fgets($st1);

switch ($n):

case 1:

       echo “enter the first stringn”;

       $str1=fopen(“php://stdin”,”r”);

       $txt1=fgets($str1);

       echo “enter the first stringn”;

       $str2=fopen(“php://stdin”,”r”);

       $txt2=fgets($str2);

       echo $txt1 . ” ” . $txt2;

      break;

case 2:

       echo “enter the stringn”;

       $str3=fopen(“php://stdin”,”r”);

       $txt3=fgets($str3);

       echo strlen($txt3);

       break;

 case 3:

       echo “enter the stringn”;

       $str4=fopen(“php://stdin”,”r”);

       $txt4=fgets($str4);

       echo “enter the string whose position need to be foundn”;

$str5=fopen(“php://stdin”,”r”);

$txt5=fgets($str5);

echo strpos(“$txt4″,”$txt5”);

break;

case 4:echo “enter the stringn”;

$str5=fopen(“php://stdin”,”r”);

$txt5=fgets($str5);

echo “enter the string to be replacedn”;

$str6=fopen(“php://stdin”,”r”);

$txt6=fgets($str6);

echo “enter the replacing stringn”;

$str7=fopen(“php://stdin”,”r”);

$txt7=fgets($str7);

echo str_replace(“$txt6″,”$txt7″,”$txt5”);

break;

endswitch;

?>

OUTPUT

Enter the choice

1.string concatenation

2.srting length

3.string position

4.string replace

1

Enter the first string

Good

Enter the second string

Morning

Good morning

Enter the choice

1.string concatenation

2.string length

3.string position

4.string replace

2

Enter the string

Good

LEAVE A REPLY