Skip to content
Related Articles

Related Articles

Improve Article

Print consecutive characters together in a line

  • Difficulty Level : Easy
  • Last Updated : 18 Jun, 2021
Geek Week

Given a sequence of characters, print consecutive sequence of characters in a line, otherwise 
print it in a new line.

Examples: 

Input  : ABCXYZACCD
Output : ABC
         XYZ
         A
         C
         CD

Input : ABCZYXACCD
Output: ABC
        ZYX
        A
        C
        CD

The idea is to traverse string from left to right. For every traversed character, print it in a line if it is consecutive to previous one, else print a new line character. 

C++




// C++ program to print consecutive characters
// together in a line.
#include <iostream>
using namespace std;
 
void print(string str)
{
    cout << str[0];
    for (int i=1; str[i]!='\0'; i++)
    {
        if ((str[i] == str[i-1]+1) ||
            (str[i] == str[i-1]-1))
            cout << str[i];
        else
            cout << "\n" << str[i];;
    }
}
 
// Driver code
int main()
{
    string str = "ABCXYZACCD";
    print(str);
    return 0;
}

Java




// Java program to print consecutive characters
// together in a line.
class GFG {
 
    static void print(String str1) {
        char str[] = str1.toCharArray();
        System.out.print(str[0]);
        for (int i = 1; i < str.length; i++) {
            if ((str[i] == str[i - 1] + 1)
                    || (str[i] == str[i - 1] - 1)) {
                System.out.print(str[i]);
            } else {
                System.out.print("\n" + str[i]);
            }
        }
    }
 
// Driver code
    public static void main(String[] args) {
        String str = "ABCXYZACCD";
        print(str);
    }
}
 
// This code is contributed by 29AjayKumar

Python3




# Python3 program to print consecutive characters
# together in a line.
def _print(string):
    print(string[0], end = "")
 
    for i in range(1, len(string)):
        if (ord(string[i]) == ord(string[i - 1]) + 1 or
            ord(string[i]) == ord(string[i - 1]) - 1):
            print(string[i], end = "")
        else:
            print()
            print(string[i], end = "")
 
# Driver Code
if __name__ == "__main__":
 
    string = "ABCXYZACCD"
    _print(string)
 
# This code is contributed by
# sanjeev2552

C#




// C# program to print consecutive characters
// together in a line.
using System;
 
class GFG
{
 
    static void print(String str1)
    {
        char []str = str1.ToCharArray();
        Console.Write(str[0]);
        for (int i = 1; i < str.Length; i++)
        {
            if ((str[i] == str[i - 1] + 1)
                    || (str[i] == str[i - 1] - 1))
            {
                Console.Write(str[i]);
            }
            else
            {
                Console.Write("\n" + str[i]);
            }
        }
    }
 
    // Driver code
    public static void Main()
    {
        String str = "ABCXYZACCD";
        print(str);
    }
}
 
// This code is contributed by PrinciRaj1992

Javascript




<script>
 
// Javascript program to print consecutive
// characters together in a line.
function print(str1)
{
    let str = str1.split("");
    document.write(str[0]);
     
    for(let i = 1; i < str.length; i++)
    {
        if ((str[i].charCodeAt(0) ==
         str[i - 1].charCodeAt(0) + 1) ||
            (str[i].charCodeAt(0) ==
         str[i - 1].charCodeAt(0) - 1))
        {
            document.write(str[i]);
        }
        else
        {
            document.write("<br>" + str[i]);
        }
    }
}
 
// Driver code
let str = "ABCXYZACCD";
print(str);
 
// This code is contributed by rag2127
 
</script>

Output: 

ABC
XYZ
A
C
CD

Time Complexity : O(n) 



This article is contributed by Rakesh Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :