N-th character in the string made by concatenating natural numbers

Given an integer N, the task is to find the N-th character in the string made by concatenating natural numbers (Integers beginning from 1). The starting sting will be “12345678910111213..”.

Examples:

Input: N = 3 
Output: 3
3rd character in the string "12345678910111213.." is 3.

Input: N = 11
Output: 0
11th character in the string "12345678910111213..." is 0

The idea is to generate the required string until string length does exceeds N.

  • Initialize a Null string and c=1
  • Add the c to the string by type-casting it to a character
  • If c is a single digit number, append it to the string
  • if c is greater than 9, then store it in a temporary string and reverse it and append to the original string
  • If at any moment the string length exceeds N, then return s[n-1].

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the N-th character
// in the string "1234567891011.."
#include <bits/stdc++.h>
using namespace std;
  
// Function that returns the N-th character
char NthCharacter(int n)
{
    // initially null string
    string s = "";
  
    // starting integer
    int c = 1;
  
    // add integers in string
    for (int i = 1;; i++) {
  
        // one digit numbers added
        if (c < 10)
            s += char(48 + c);
  
        // more than 1 digit number, generate
        // equivalent number in a string s1
        // and concatenate s1 into s.
        else 
        {
            string s1 = "";
            int dup = c;
  
            // add the number in string
            while (dup) {
                s1 += char((dup % 10) + 48);
                dup /= 10;
            }
  
            // reverse the string
            reverse(s1.begin(), s1.end());
  
            // attach the number
            s += s1;
        }
        c++;
  
        // if the length exceeds N
        if (s.length() >= n) {
            return s[n - 1];
        }
    }
}
  
// Driver Code
int main()
{
    int n = 11;
  
    cout << NthCharacter(n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the N-th character
// in the string "1234567891011.."
  
  
class GFG
{
    // Function that returns the N-th character
    static char NthCharacter(int n)
    {
        // initially null string
        String s = "";
      
        // starting integer
        int c = 1;
      
        // add integers in string
        for (int i = 1;; i++) {
      
            // one digit numbers added
            if (c < 10)
                s += Integer.toString(c);
      
            // more than 1 digit number, generate
            // equivalent number in a string s1
            // and concatenate s1 into s.
            else
            {
                String s1 = "";
                int dup = c;
      
                // add the number in string
                while (dup >0) {
                    s1 += Integer.toString(dup % 10);
                    dup /= 10;
                }
            
                // reverse the string
                StringBuilder temp = new StringBuilder(); 
                temp.append(s1); 
                temp = temp.reverse(); 
                  
                // attach the number
                s += temp;
            }
            c++;
      
            // if the length exceeds N
            if (s.length() >= n) {
                return s.charAt(n - 1);
            }
        }
    }
      
    // Driver Code
    public static void main(String []args)
    {
        int n = 11;
      
        System.out.println( NthCharacter(n));
      
          
    }
  
}
  
// This article is contributed by ihritik

chevron_right


Python 3

# Python 3 program to find the N-th character
# in the string “1234567891011..”

# Function that returns the N-th character
def NthCharacter(n):

# initially null string
s = “”

# starting integer
c = 1

# add integers in string
while(True) :

# one digit numbers added
if (c < 10): s += chr(48 + c) # more than 1 digit number, generate # equivalent number in a string s1 # and concatenate s1 into s. else: s1 = "" dup = c # add the number in string while (dup > 0):
s1 += chr((dup % 10) + 48)
dup //= 10

# reverse the string
s1 = “”.join(reversed(s1))

# attach the number
s += s1
c += 1

# if the length exceeds N
if (len(s) >= n):
return s[n – 1]

# Driver Code
if __name__ == “__main__”:

n = 11
print(NthCharacter(n))

# This code is contributed by ita_c

Output:

0


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ihritik, Ita_c



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.