Abdullah  Kozey

Abdullah Kozey


String — An array of characters with ‘\0’

What if a word or a sentence is needed to be stored in a variable? One way to do this is using a character array. There is a more convenient way, to store them as strings.

What is a string?

A string is an array of characters that is terminated with a null terminator. The null terminator is a special character ‘\0’, which has the numerical value of 0. It is used to represent emptiness. So, in the case of a string, the null terminator denotes where the string ends. So, can’t we just use an empty char to terminate? No, char cannot accept empty values. It does not work like that. That is why a null terminator is used.

Comparing strings

To compare strings, we need to include ‘string.h’.

#include <string.h>

This header file provides some functions or ways to do some operations on strings. The ‘strcmp()’ is used to compare strings.

strcmp(string1, string2);

Assigning to a string

String assignment does not work in the usual way.

char myCar[] = "BMW";
char myNewCar[] = "Tesla";

myCar = myNewCar;
printf("%s", myCar);

Reading strings

Reading string input from the user is a bit different.

char name[30];
scanf("%s", name);

Usually, an ampersand is placed before the variable to which the input value is to be stored.

int num;
scanf("%d", &num);

Sample Quiz Program

Let’s make a small quiz program from all the knowledge we acquired through this and previous articles.

#tutorial #c-programming #programming-tips #programming

What is GEEK

Buddha Community

String — An array of characters with ‘\0’

Generate a string from an array of alphanumeric strings based on given conditions

Given an array of strings arr[] where each string is of the form “name:number” and a character T as input, the task is to generate a new string based on the following conditions:

  • In each string find the maximum digit in “number” which is less than or equal to the length of the string “name”.
  • If any such digit d is obtained, then append character at index d of the string name to the output string. Otherwise, append character T to the output string.


Input:_ arr[] = {“Robert:36787”, “Tina:68721”, “Jo:56389”}, T = ‘X’_

Output:_ tiX_


For the first string “Robert:36787”: Length of “Robert” is 6. Since 6 is present in the string “36787”, 6th character of “Robert”, i.e. t is appended to the answer.

For the second string “Tina:68721”: Length of “Tina” is 4. The highest number less than equal to 4, which is present in “68721” is 2. Therefore, 2nd character of “Tina”, i.e. i is appended to the answer.

For the third string “Jo:56389”: Length of “Jo” is 2. Since no number less than equal to 2 is present in “56389”, T( = ‘X’) is appended to the answer.

Therefore, the final string after the above operations is “tiX”.

_Input: _arr[] = {“Geeks:89167”, “gfg:68795”}, T = ‘X’

Output:_ GX_


For the first string “Geeks:89167”, length of “Geeks” = 5 and the “89167” number has digit 1 which is less than 5.

So, the resultant string will have the character at the 1st position of the name, which is ‘G’.

For the second string “gfg:68795”, the length of “gfg” = 3, and the “68795” doesn’t have a digit less than or equals to 3.

So, the resultant string will have the character T.

Therefore, the final string after the above operations is “GX”.

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: To solve the problem follow the steps given below:

  1. Traverse through the array of strings and split each string around “:“. The first part contains the name and second part contains the number.
  2. Store the length of the name in a variable and find the maximum digit less than or equal to the length of number.
  3. If any such digit found is found, extract the character at that index of name and append to the resultant string. Otherwise, append T to the resultant string.
  4. Print the resultant string after repeating the above operations for all the strings in the array.

Below is the implementation of the above approach:


// Java program for the above approach

**import** java.io.*;

**class** GFG {

// Function to generate required string

**public** **static** String

generatePassword(String s[], **char** T)


// To store the result

StringBuilder result

= **new** StringBuilder();

**for** (String currentString : s) {

// Split name and number

String person[]

= currentString.split(``":"``);

String name = person[``0``];

String number = person[``1``];

**int** n = name.length();

// Stores the maximum number

// less than or equal to the

// length of name

**int** max = 0``;

**for** (``**int** i = 0``;

i < number.length(); i++) {

// Check for number by parsing

// it to integer if it is greater

// than max number so far

**int** temp = Integer.parseInt(


**if** (temp > max && temp <= n)

max = temp;


// Check if no such number is

// found then we append X

// to the result.

**if** (max == 0``)


// Otherwise


// Append max index

// of the name



name.charAt(max - 1``)));


// Return the final string

**return** result.toString();


// Driver Code

**public** **static** **void**

main(String[] args)


String arr[] = { "Geeks:89167"``,

"gfg:68795" };

**char** T = 'X'``;

// Function Call


generatePassword(arr, T));





Time Complexity:_ O(N)_

Auxiliary Space:_ O(1)_

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

#arrays #searching #strings #strings

Madilyn  Kihn

Madilyn Kihn


Convert String To Array Using Javascript Split Method

The easiest approach to use javascript built-in method String.split().

#javascript #javascript string #string to array #morioh #array

Joseph  Murray

Joseph Murray


String Array In Java: Java String Array With Coding Examples

In Java, Array refers to a crucial data structure used to collect and store multiple data types from primitive to user-defined. String array is the array of various objects where each of its elements is a string. Users can perform several operations on these components, like adding a component, sorting, joining, searching, splitting, etc.

Introduction To A String Array In Java

It is possible to have an array with strings in Java as its derived elements. This means that users can define ‘String Array’ as an array that holds a certain number of string values or strings. In other words, it refers to a structure that is widely used in Java for having the string value. For instance, even the number of arguments of the prime function in Java refers to a string array.

#software development #array in java #java #string array

Joseph  Murray

Joseph Murray


How to Print an Array in Java


Printing an array is a quick way to give us visibility on the values of the contents inside. Sometimes the array values are the desired output of the program.

In this article, we’ll take a look at how to print an array in Java using four different ways.

While the “best way” depends on what your program needs to do, we begin with the simplest method for printing and then show more verbose ways to do it.

#java #array #how to print an array in java #array in java #print an array in java #print

What is difference between String and string in C#

Are you confused about System.String and string in C#? What is the difference between String and string in C#? And how to choose between string and System.String? In this article, I am going to show you all the differences between string and System.String in C## with code examples. .

What is the difference between System.String and string in C#?

Basically, there is no difference between string and String in C#. “string” is just an alias of System.String and both are compiled in the same manner. String stands for System.String and it is a .NET Framework type. “string” is an alias in the C## language for System.String. Both of them are compiled to System.String in IL (Intermediate Language), so there is no difference.

#c# #string #string and string