Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Welcome to Interviewbit, help us create the best experience for you!

Currently, You are a:

Few details about your education

College/University *
Enter the name of your college
Branch *
Year of completion *

Few details about your education

College/University *
Enter the name of your college
Branch *
Year of completion *

Few details about your career...

Current Company *
Enter company name *
Experience *

You're all set!

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

Strings

Go to Problems

Level 1

Jump to Level 2

Level 2

Jump to Level 3

Level 3

Jump to Level 4

Level 4

Jump to Level 5

Level 5

Jump to Level 6

Level 6

Jump to Level 7

Level 7

Jump to Level 8

Level 8

Be a Code Ninja!

String Implementation Details

C :
String declaration :
    char A[1000]; // creates a character array of size 1000. 
    char *A = (char *) malloc(1000 * sizeof(char)); // creates a character array of size 1000
Accessing ith element :
    A[i] 
The end of string :
    Strings have to end with a '\0' character. 
    All C input functions like scanf do it automatically. 
    Direct string assignments like `char A[20] = "Hello";` does it automatically as well.
Length of string :
    strlen(A); // O(n) operation where n is length of string
C++
C style char arrays work in C++ as well.  However, C++ provides string which is much more powerful than C arrays. We will be using C++ strings in most of the problems on this judge. 
String declaration :
    string A; // declares an empty string
    string A = "Hello"; // declares string initialized to "Hello".
Accessing ith element :
    A[i] // O(1)
Size ( number of elements ) of the string :
    A.length()  // O(1)
Appending to the string :
    A += "Hello"; // Appends Hello to the string. O(n) operation
    A.push_back('H'); // Append character H to the string. O(1) operation. 
JAVA :

Java has strings as well which are very similar to C++ string. However java strings are immutable. As such, if you plan to keep appending to the string, then better use StringBuilder.

Array declaration :

    String A = new String(); // declares an empty string. O(1)
    String A = new String("Hello"); // declares string initialized to "Hello"
    OR for mutable string : 
    StringBuilder A = new StringBuilder(); // empty string
    StringBuilder A = new StringBuilder("Hello"); // stringbuilder initialized to "Hello".
Accessing ith element :
    A.charAt(i)     // O(1). Works for both String and StringBuilder
Size of the string :
    A.length()  // O(1) operation
Adding characters to the string :
    String : A.concat("Hello"); // O(n) operation. Creates another copy of the string as string is immutable. 
    StringBuilder : A.append("Hello"); // O(l) operation where l is the length of the string being appended. Much more efficient. 
PYTHON

Python has string which is very similar to java string. Its immutable.

String declaration :
    A = "" #  empty string
    A = "Hello" #  string initialized to "Hello"
Accessing the ith element :
    A[i]  # O(1)
Adding chars to the string :
    A = A + "Hello" #  O(n) operation and a new copy is created

An alternative way if you are going to do a lot of appends :

        l = []
        l.append('string1')
        l.append('string2')
        l.append('string3')
        A = ''.join(l)
Size of the string :
    len(A)  // O(1)

Serious about Learning Programming ?

Learn this and a lot more with Scaler Academy's industry vetted curriculum which covers Data Structures & Algorithms in depth.

Strings Problems

String search
Problem Score Companies Time Status
Amazing Subarrays 150
26:39
Implement StrStr 225 33:14
Stringoholics 300
65:20
String math
Problem Score Companies Time Status
Salutes 200 18:34
Integer To Roman 250 42:10
Roman To Integer 250 33:02
Add Binary Strings 300
40:40
Power of 2 350 63:24
Multiply Strings 375
64:50
Words
Problem Score Companies Time Status
Length of Last Word 225 17:54
Reverse the String 250 36:31
Pretty print
Problem Score Companies Time Status
Zigzag String 300
52:40
Justified Text 300 78:33
Pretty Json 400
64:23

Additional Practice

Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test