Strings- Basic String Concepts
Strings, which are sequences of characters are data types, in programming languages. They serve the purpose of representing and manipulating information. Common tasks performed on strings include combining them extracting substrings determining their length and comparing them.
In C and C++ strings are represented as arrays of characters that end with a character (‘\0’). On the hand in Python and Java strings are treated as objects with built in methods, for performing operations.
#include <iostream>
#include <string>
int main()
{
// String declaration and initialization
std::string myString = "Hello, w3wiki!";
// String length
std::cout << "Length of the string: "
<< myString.length() << std::endl;
// String concatenation
std::string anotherString = " How are you?";
myString += anotherString;
std::cout << "Concatenated string: " << myString
<< std::endl;
// Accessing individual characters
std::cout << "First character: " << myString[0]
<< std::endl;
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
// String declaration and initialization
char myString[] = "Hello, w3wiki!";
// String length
printf("Length of the string: %lu\n", strlen(myString));
// String concatenation
char anotherString[] = " How are you?";
strcat(myString, anotherString);
printf("Concatenated string: %s\n", myString);
// Accessing individual characters
printf("First character: %c\n", myString[0]);
return 0;
}
public class StringExample {
public static void main(String[] args)
{
// String declaration and initialization
String myString = "Hello, w3wiki!";
// String length
System.out.println("Length of the string: "
+ myString.length());
// String concatenation
String anotherString = " How are you?";
myString = myString.concat(anotherString);
System.out.println("Concatenated string: "
+ myString);
// Accessing individual characters
System.out.println("First character: "
+ myString.charAt(0));
}
}
# String declaration and initialization
my_string = "Hello, w3wiki!"
# String length
print("Length of the string:", len(my_string))
# String concatenation
another_string = " How are you?"
my_string += another_string
print("Concatenated string:", my_string)
# Accessing individual characters
print("First character:", my_string[0])
using System;
class StringExample {
static void Main()
{
// String declaration and initialization
string myString = "Hello, w3wiki!";
// String length
Console.WriteLine("Length of the string: "
+ myString.Length);
// String concatenation
string anotherString = " How are you?";
myString += anotherString;
Console.WriteLine("Concatenated string: "
+ myString);
// Accessing individual characters
Console.WriteLine("First character: "
+ myString[0]);
}
}
class StringExample {
static main() {
// String declaration and initialization
let myString = "Hello, w3wiki!";
// String length
console.log("Length of the string: " + myString.length);
// String concatenation
let anotherString = " How are you?";
myString += anotherString;
console.log("Concatenated string: " + myString);
// Accessing individual characters
console.log("First character: " + myString.charAt(0));
}
}
// Call the main method
StringExample.main();
Output
Length of the string: 21 Concatenated string: Hello, w3wiki! How are you? First character: H
How to Start Coding
In this ever-evolving digital landscape, the ability to code is not just a valuable asset but a gateway to creative expression and innovation. This brief guide will outline practical steps and essential principles to guide aspiring learners on their path to mastering the art of coding.
Table of Content
- Introduction, to Basic Syntax. Hello World
- Understanding Data Types
- Variables and Constants
- Keywords
- Operators
- Decision Making statements
- Loops
- Numbers
- Characters
- Arrays
- Strings- Basic String Concepts
- Functions
Contact Us