String Introduction
What is a String?
A string is an ordered sequence of characters, used to represent text like names, sentences, or file paths. In most languages, strings behave like read-only or specially-managed sequences with their own set of built-in operations.
Example: What is a String?
#include <iostream>
using namespace std;
int main() {
string s = "hello";
cout << "String: " << s << ", type holds ordered characters" << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
String s = "hello";
System.out.println("String: " + s + ", type holds ordered characters");
}
}
s = "hello"
print("String:", s, ", type holds ordered characters")
#include <stdio.h>
int main() {
char s[] = "hello";
printf("String: %s, type holds ordered characters\n", s);
return 0;
}
Login to try C/C++/Java code in the editor
String Length
A string's length tells you exactly how many characters it contains, which is essential for writing loops that stay within bounds and for validating input, such as checking a password meets a minimum length.
Example: String Length
#include <iostream>
using namespace std;
int main() {
string s = "password123";
cout << "Length: " << s.length() << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
String s = "password123";
System.out.println("Length: " + s.length());
}
}
s = "password123"
print("Length:", len(s))
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "password123";
printf("Length: %lu\n", strlen(s));
return 0;
}
Login to try C/C++/Java code in the editor
Access Characters
Each character in a string has a position called its index, and in most languages that indexing starts at 0, so a string of length n has valid indexes from 0 through n−1, exactly like an array.
Example: Access Characters
#include <iostream>
using namespace std;
int main() {
string s = "hello";
cout << "s[0]=" << s[0] << ", s[4]=" << s[4] << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
String s = "hello";
System.out.println("s.charAt(0)=" + s.charAt(0) + ", s.charAt(4)=" + s.charAt(4));
}
}
s = "hello"
print(f"s[0]={s[0]}, s[4]={s[4]}")
#include <stdio.h>
int main() {
char s[] = "hello";
printf("s[0]=%c, s[4]=%c\n", s[0], s[4]);
return 0;
}
Login to try C/C++/Java code in the editor
Read a String
Programs commonly read string input from a user, a file, or a network request, and the exact method (like input() in Python or Scanner in Java) depends on the language and where the text is coming from.
Example: Read a String
#include <iostream>
#include <sstream>
using namespace std;
int main() {
istringstream input("Alice"); // stands in for real cin >> s
string s;
input >> s;
cout << "Read: " << s << endl;
return 0;
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner("Alice"); // stands in for new Scanner(System.in)
String s = sc.next();
System.out.println("Read: " + s);
}
}
s = "Alice" # stands in for input()
print("Read:", s)
#include <stdio.h>
int main() {
char s[20];
sscanf("Alice", "%s", s); // stands in for scanf("%s", s)
printf("Read: %s\n", s);
return 0;
}
Login to try C/C++/Java code in the editor
Basic String Practice
Good starting exercises are counting how many times a character appears, or printing each character with its index, since these build the same indexing skills you'll need for more advanced string algorithms.
Example: Basic String Practice
#include <iostream>
using namespace std;
int main() {
string s = "banana";
int count = 0;
for (char c : s) if (c == 'a') count++;
cout << "'a' appears " << count << " times" << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
String s = "banana";
int count = 0;
for (char c : s.toCharArray()) if (c == 'a') count++;
System.out.println("'a' appears " + count + " times");
}
}
s = "banana"
print("'a' appears", s.count('a'), "times")
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "banana";
int count = 0;
for (int i = 0; i < strlen(s); i++) if (s[i] == 'a') count++;
printf("'a' appears %d times\n", count);
return 0;
}
Login to try C/C++/Java code in the editor
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: