String
String
OPERATIONS.
AIM :
Algorithm :
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 5: Store the output of the selected operation in a variable called result.
STEP 7: Optionally, provide the user with the option to perform another operation or end
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: