
Solve 50 PHP String Programming Questions
Q1: Write a function to calculate the length of a string.
Input:stringLength("Hello World")
Expected Output:
11
Q2: Write a program to convert a string to uppercase.
Input:convertToUpper("hello world")
Expected Output:
HELLO WORLD
Q3: Write a program to convert a string to lowercase.
Input:convertToLower("HELLO WORLD")
Expected Output:
hello world
Q4: Write a function to reverse a string.
Input:reverseString("PHP")
Expected Output:
PHP
Q5: Write a function to count the number of vowels in a string.
Input:countVowels("Hello World")
Expected Output:
3
Q6: Write a function to count the number of words in a string.
Input:countWords("PHP is awesome!")
Expected Output:
3
Q7: Write a program to find the position of the first occurrence of a substring.
Input:findSubstring("Hello World", "World")
Expected Output:
6
Q8: Write a program to check if a string contains a specific substring.
Input:containsSubstring("Hello World", "World")
Expected Output:
True
Q9: Write a function to replace all occurrences of a word in a string with another word.
Input:replaceWord("Hello World", "World", "PHP")
Expected Output:
Hello PHP
Q10: Write a program to split a string into an array using a delimiter.
Input:splitString("one,two,three", ",")
Expected Output:
["one", "two", "three"]
Q11: Write a function to join an array of strings into a single string using a delimiter.
Input:joinArray(["one", "two", "three"], ", ")
Expected Output:
one, two, three
Q12: Write a function to remove all spaces from a string.
Input:removeSpaces("Hello World")
Expected Output:
HelloWorld
Q13: Write a program to check if a string starts with a specific substring.
Input:startsWith("Hello World", "Hello")
Expected Output:
True
Q14: Write a program to check if a string ends with a specific substring.
Input:endsWith("Hello World", "World")
Expected Output:
True
Q15: Write a program to extract a substring from a string.
Input:extractSubstring("Hello World", 6, 5)
Expected Output:
World
Q16: Write a function to repeat a string a given number of times.
Input:repeatString("PHP", 3)
Expected Output:
PHPPHPPHP
Q17: Write a program to capitalize the first letter of a string.
Input:capitalizeFirstLetter("hello world")
Expected Output:
Hello world
Q18: Write a program to capitalize the first letter of each word in a string.
Input:capitalizeWords("hello world")
Expected Output:
Hello World
Q19: Write a program to remove all special characters from a string.
Input:removeSpecialCharacters("Hello@World!")
Expected Output:
HelloWorld
Q20: Write a function to check if a string is a palindrome.
Input:isPalindrome("madam")
Expected Output:
True
Q21: Write a function to calculate the ASCII value of the first character in a string.
Input:asciiValue("A")
Expected Output:
65
Q22: Write a program to shuffle the characters in a string.
Input:shuffleString("hello")
Expected Output:
(e.g., "loleh", output varies)
Q23: Write a function to find the last occurrence of a character in a string.
Input:findLastOccurrence("Hello World", "o")
Expected Output:
7
Q24: Write a program to pad a string to a specific length with spaces.
Input:padString("Hello", 10)
Expected Output:
Hello
Q25: Write a program to check if a string is empty.
Input:isEmpty("")
Expected Output:
True
Q26: Write a program to convert the first character of a string to uppercase and the rest to lowercase.
Input:ucfirstLowerRest("hELLO wORLD")
Expected Output:
Hello world
Q27: Write a program to find the number of occurrences of a character in a string.
Input:countCharacterOccurrences("Hello World", "o")
Expected Output:
2
Q28: Write a function to truncate a string to a specified number of characters and add "..." at the end.
Input:truncateString("Hello World", 5)
Expected Output:
Hello...
Q29: Write a program to compare two strings for equality (case-sensitive).
Input:areStringsEqual("Hello", "hello")
Expected Output:
False
Q30: Write a program to compare two strings for equality (case-insensitive).
Input:areStringsEqualIgnoreCase("Hello", "hello")
Expected Output:
True
Q31: Write a program to encode a string in Base64.
Input:encodeBase64("Hello World")
Expected Output:
SGVsbG8gV29ybGQ=
Q32: Write a program to decode a Base64 string.
Input:decodeBase64("SGVsbG8gV29ybGQ=")
Expected Output:
Hello World
Q33: Write a program to check if a string contains only numbers.
Input:isNumericString("12345")
Expected Output:
True
Q34: Write a program to find the first non-repeated character in a string.
Input:findFirstNonRepeatedCharacter("swiss")
Expected Output:
w
Q35: Write a program to convert a string into an array of characters.
Input:stringToArray("hello")
Expected Output:
["h", "e", "l", "l", "o"]
Q36: Write a program to remove a specific word from a string.
Input:removeWord("Hello World", "World")
Expected Output:
Hello
Q37: Write a program to calculate the percentage of alphabetic characters in a string.
Input:percentageAlphabetic("123abcXYZ!")
Expected Output:
50%
Q38: Write a program to insert a string into another string at a specified position.
Input:insertString("Hello World", "PHP ", 6)
Expected Output:
Hello PHP World
Q39: Write a program to find the longest word in a string.
Input:findLongestWord("PHP is amazing")
Expected Output:
amazing
Q40: Write a function to repeat each character of a string twice.
Input:repeatCharacters("hello")
Expected Output:
hheelllloo
Q41: Write a program to remove duplicate characters from a string.
Input:removeDuplicates("aabbcc")
Expected Output:
abc
Q42: Write a program to check if two strings are anagrams.
Input:areAnagrams("listen", "silent")
Expected Output:
True
Q43: Write a function to mask all but the last 4 characters of a string with asterisks.
Input:maskString("123456789")
Expected Output:
*6789
Q44: Write a program to count the number of uppercase letters in a string.
Input:countUppercaseLetters("Hello World")
Expected Output:
2
Q45: Write a program to count the number of lowercase letters in a string.
Input:countLowercaseLetters("Hello World")
Expected Output:
8
Q46: Write a program to remove all numeric digits from a string.
Input:removeDigits("abc123xyz")
Expected Output:
abcxyz
Q47: Write a program to check if a string is all uppercase.
Input:isAllUppercase("HELLO")
Expected Output:
True
Q48: Write a program to check if a string is all lowercase.
Input:isAllLowercase("hello")
Expected Output:
True
Q49: Write a function to find the shortest word in a string.
Input:findShortestWord("PHP is amazing")
Expected Output:
is
Q50: Write a program to append one string to another.
Input:appendStrings("Hello", " World")
Expected Output:
Hello World