String Practice
Practice with strings…
Below are some short exercises designed to help you practice working with strings. As in the past, try the “less comfortable” ones if you’re still feeling a little less comfortable with your understanding. Tackle the “more comfortable” problems if you want a little more of a challenge.
Less Comfortable
Choose any two of the following:
Reverse
- Write a program
reverse.cthat takes a string as input and reverses it.
Example
$ ./reverse
Text: This is forward.
Reverse: .drawrof si sihT
Checking
check50 scienceacademy/problems/2025ap/reverse
Submitting
submit50 scienceacademy/problems/2025ap/reverse
Alphabetical
- Write a program
alphabetical.cthat prints “Yes” if a lowercase string’s characters are in alphabetical order, and “No” if they are not.
Example
$ ./alphabetical
Text: abcdefg
Yes
$ ./alphabetical
Text: gfedcba
No
Checking
check50 scienceacademy/problems/2025ap/alphabetical
Submitting
submit50 scienceacademy/problems/2025ap/alphabetical
Palindrome
- Write a program
palindrome.cthat takes a string as input and determines whether it’s a palindrome (the same backwards and forwards).
Example
$ ./palindrome
Text: racecar
PALINDROME
$ ./palindrome
Text: jellyfish
NOT PALINDROME
Checking
check50 scienceacademy/problems/2025ap/palindrome
Submitting
submit50 scienceacademy/problems/2025ap/palindrome
More Comfortable
Palindrome2
- Write a program
palindrome2.cthat works just likepalindrome.cabove but ignores spaces, so that it will identify palindromes liketaco cat.
Checking
check50 scienceacademy/problems/2025ap/palindrome2
Submitting
submit50 scienceacademy/problems/2025ap/palindrome2
Anagram
- Write a program
anagram.cthat takes two words as input and determines whether they are anagrams of one another.- If the two words are an exact match (ignoring case), output “EXACT MATCH”.
- If either word contains non-alphabetic characters, output “Alphabetic characters only” and return
1. - Otherwise, the program should check to see if the letters in the first word can be rearranged to form the second word (ignoring case). If so, the program should output “ANAGRAM”; if not, output “NOT ANAGRAM”.
Examples
$ ./anagram
Word 1: listen
Word 2: silent
ANAGRAM
$ ./anagram
Word 1: program
Word 2: silent
NOT ANAGRAM
$ ./anagram
Word 1: program
Word 2: program
EXACT MATCH
$ ./anagram
Word 1: this
Word 2: cs50
Alphabetic characters only.
Checking
check50 scienceacademy/problems/2025ap/anagram
Submitting
submit50 scienceacademy/problems/2025ap/anagram