String algorithms
Reverse a String
java
public String reverse(String str) {
String reversed = "";
for (int i = str.length() - 1; i >= 0; i--) {
reversed += str.substring(i, i + 1);
}
return reversed;
}
reverse("Hello") // "olleH"
reverse("abcd") // "dcba"
Check if a String is a palindrome
java
public boolean isPalindrome(String str) {
String reversed = "";
for (int i = str.length() - 1; i >= 0; i--) {
reversed += str.substring(i, i + 1);
}
return str.equals(reversed);
}
isPalindrome("racecar") // true
isPalindrome("hello") // false
Count occurrences of a substring
java
public int countOccurrences(String text, String target) {
int count = 0;
for (int i = 0; i <= text.length() - target.length(); i++) {
if (text.substring(i, i + target.length()).equals(target)) {
count++;
}
}
return count;
}
countOccurrences("banana", "an") // 2
countOccurrences("hello", "ll") // 1
Replace characters
java
public String replaceChar(String str, String oldCh, String newCh) {
String result = "";
for (int i = 0; i < str.length(); i++) {
String ch = str.substring(i, i + 1);
if (ch.equals(oldCh)) {
result += newCh;
} else {
result += ch;
}
}
return result;
}
replaceChar("hello", "l", "r") // "herro"
Numeric algorithms
Sum and average
java
// Sum of first n positive integers
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
double average = (double) sum / n; // cast to avoid integer division!
Finding minimum and maximum
java
// Find the largest digit in a number
public int largestDigit(int num) {
num = Math.abs(num); // handle negatives
int max = 0;
while (num > 0) {
int digit = num % 10; // extract last digit
if (digit > max) {
max = digit;
}
num /= 10; // remove last digit
}
return max;
}
largestDigit(38274) // 8
largestDigit(11111) // 1
Check if a number is prime
java
public boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false; // found a factor → not prime
}
}
return true; // no factors found → prime
}
isPrime(7) // true
isPrime(12) // false (12 % 2 == 0)
isPrime(1) // false
Sum of digits
java
public int digitSum(int num) {
num = Math.abs(num);
int sum = 0;
while (num > 0) {
sum += num % 10; // add last digit
num /= 10; // remove last digit
}
return sum;
}
digitSum(1234) // 1 + 2 + 3 + 4 = 10
digitSum(999) // 9 + 9 + 9 = 27
Counting pattern
java
// Count uppercase letters in a string
public int countUppercase(String str) {
int count = 0;
for (int i = 0; i < str.length(); i++) {
String ch = str.substring(i, i + 1);
if (ch.equals(ch.toUpperCase()) && !ch.equals(ch.toLowerCase())) {
count++;
}
}
return count;
}
countUppercase("Hello World") // 2 (H, W)
Building results
java
// Keep only the letters (remove digits, punctuation)
public String lettersOnly(String str) {
String result = "";
String letters = "abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < str.length(); i++) {
String ch = str.substring(i, i + 1).toLowerCase();
if (letters.indexOf(ch) >= 0) {
result += str.substring(i, i + 1); // keep original case
}
}
return result;
}
lettersOnly("H3llo W0rld!") // "HlloWrld"
AP algorithm summary
AP Exam Tips
- •
- •
- •
- •
- •
Common Mistakes
- •
- •
- •
- •