Finding the length/size of a string
To find the length of the String, we need:
- A string for which the length/size is to be determined (say “str”)
Below is the implementation of the above approach:
// C++ program to find length
// of a string
#include <iostream>
#include <string.h>
using namespace std;
// Driver code
int main()
{
// String obj
string str = "w3wiki";
// size of string object using size() method
cout << str.size() << endl;
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
// String
char str[] = "w3wiki";
// Length of string using strlen() function
int length = strlen(str);
printf("%d\n", length);
return 0;
}
public class Main {
public static void main(String[] args)
{
// String object
String str = "w3wiki";
// Size of string object using length() method
System.out.println(str.length());
}
}
// This code is contributed by Utkarsh
# Main function
def main():
# String object
str = "w3wiki"
# Size of string object using len() function
print(len(str))
# Calling the main function
if __name__ == "__main__":
main()
// JavaScript program to find length
// of a string
// String
let str = "w3wiki";
// size of string using length property
console.log(str.length);
Output
13
Basic String Operations with Implementation
In this post, we will look into some of the basic String operations such as:
- Accessing characters by index in a string.
- Inserting character into a String.
- Modifying character in String
- Deletion of Character in String
- Concatenating strings (combining multiple strings into one).
- Finding the length of a string
- Comparing strings for equality or lexicographical order
Let us consider the basic String operations one by one.
Contact Us