0% found this document useful (0 votes)
6 views5 pages

String

Uploaded by

23pca005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

String

Uploaded by

23pca005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

CREATE A PHP PROGRAM TO PERFORM STRING

OPERATIONS.

AIM :

To create a PHP Program to perform String operations.

Algorithm :

STEP 1: Show a list of 15 string operations for user selection.

STEP 2: Prompt the user to enter a string, a number (1-15) for the operation, an optional
word, and an optional replacement word.

STEP 3: Store the user inputs in variables: str, n, word, and replace.

STEP 4: Based on the value of n, perform the corresponding string operation:

STEP 5: Store the output of the selected operation in a variable called result.

STEP 6: Output the result to the user.

STEP 7: Optionally, provide the user with the option to perform another operation or end
the program.

STEP 8: Conclude the program.

CODING :

<html>
<head>
<title>switch case with string operations</title>
</head>
<body>
<h1>String operations</h1>
<b>Select the process</b>
<p>
1. Reverse a string<br>
2. Length of a string<br>
3. String to upper case<br>
4. String to lower case<br>
5. Number of words in a string<br>
6. Shuffle the string<br>
7. Repeat string 5 times<br>
8. Replace a word<br>
9. Count number of occurrences of a specific character<br>
10. Reverse each word in the string<br>
11. Convert string to title case<br>
12. Remove all vowels from the string<br>
13. Append a string<br>
14. Check if the string starts with a specific word<br>
15. Check if the string is a palindrome<br>
</p>
<form action=" " method="post">
Enter a String:
<input type="text" name="string">
Enter a number:
<input type="number" name="number" min=1 max=17>
Enter a word (if needed):
<input type="text" name="word" placeholder="Optional">
Replacement word (for case 9):
<input type="text" name="replace" placeholder="Optional">
<input type="submit" name="submit">
</form>

<?php
$str = $result = ' ';
$n = 0;
if (isset($_POST['submit'])) {
$str = $_POST['string'];
$n = $_POST['number'];
$word = isset($_POST['word']) ? $_POST['word'] : '';
$replace = isset($_POST['replace']) ? $_POST['replace'] : '';

switch ($n) {
case '1':
$result = strrev($str);
break;
case '2':
$result = strlen($str);
break;
case '3':
$result = strtoupper($str);
break;
case '4':
$result = strtolower($str);
break;
case '5':
$result = str_word_count($str);
break;
case '6':
$result = str_shuffle($str);
break;
case '7':
$result = str_repeat($str, 5);
break;
case '8':
$result = str_replace($word, $replace, $str);
break;
case '9':
$result = substr_count($str, $word);
break;
case '10':
$words = explode(" ", $str);
$reversedWords = array_map('strrev', $words);
$result = implode(" ", $reversedWords);
break;
case '11':
$result = ucwords($str);
break;
case '12':
$result = preg_replace('/[aeiouAEIOU]/', '', $str);
break;
case '13':
$result = $str . $word;
break;
case '14':
$result = (strpos($str, $word) === 0) ? 'Yes' : 'No';
break;
case '15':
$reversedStr = strrev($str);
$result = ($str == $reversedStr) ? 'Palindrome' : 'Not a Palindrome';
break;
}
echo $result;
}
?>
</body>
</html>

OUTPUT:
RESULT:

Hence the above program is executed and verified successfully.

You might also like