Given two char c1 and c2, you need to print all the alphabets starting from c1 to c2 in a single line.
Note: Print all characters with a single space after it and all in a single line. Don't add a new line after printing.
Examples:
Input: c1 = 'a', c2 = 'z'|
Output: "a b c d e f g h i j k l m n o p q r s t u v w x y z "
Explanation: Printed alphabets from a to z.Input: c1 = 'h', c2 = 'u'
Output: "h i j k l m n o p q r s t u "
Explanation: Printed alphabets from h to u.
Table of Content
[Naive Approach] While Loop - O(n) Time and O(1) Space
The idea is to start from c1 and keep increasing the character until c2 is reached. At each step, print the current character followed by a space.
Working of Approach:
- Start with c1 as the current character.
- Continue the loop while the character is less than or equal to c2.
- Print the current character followed by a space.
- Increment the character to move to the next alphabet.
- Stop when the character becomes greater than c2.
#include <iostream>
using namespace std;
void alphabets(char c1, char c2)
{
// Traverse from c1 to c2
while (c1 <= c2)
{
cout << c1 << " ";
c1++;
}
}
int main()
{
char c1 = 'h', c2 = 'u';
alphabets(c1, c2);
return 0;
}
public class GFG {
// Traverse from c1 to c2
static void alphabets(char c1, char c2)
{
while (c1 <= c2) {
System.out.print(c1 + " ");
c1++;
}
}
public static void main(String[] args)
{
char c1 = 'h', c2 = 'u';
alphabets(c1, c2);
}
}
def alphabets(c1, c2):
# Traverse from c1 to c2
while c1 <= c2:
print(c1, end=' ')
c1 = chr(ord(c1) + 1)
if __name__ == '__main__':
c1 = 'h'
c2 = 'u'
alphabets(c1, c2)
using System;
class GFG {
// Traverse from c1 to c2
static void alphabets(char c1, char c2)
{
while (c1 <= c2) {
Console.Write(c1 + " ");
c1++;
}
}
static void Main()
{
char c1 = 'h', c2 = 'u';
alphabets(c1, c2);
}
}
function alphabets(c1, c2)
{
// Traverse from c1 to c2
while (c1 <= c2) {
process.stdout.write(c1 + "");
c1 = String.fromCharCode(c1.charCodeAt(0) + 1);
}
}
// Driver Code
let c1 = "h", c2 = "u";
alphabets(c1, c2);
Output
h i j k l m n o p q r s t u
[Expected Approach] For Loop for Character Range - O(n) Time and O(1) Space
The idea is to directly iterate from c1 to c2 using a for loop and print every character followed by a space.
Working of Approach:
- Initialize the loop variable with c1.
- Run the loop while the current character is at most c2.
- Print the current character followed by a space.
- Increment the loop variable to get the next alphabet.
- Continue until all characters from c1 to c2 are printed.
Let us understand with an example:
Input: c1 = 'h', c2 = 'u'
- Initialize c1 = 'h' and c2 = 'u', then start the loop with c = 'h'.
- Print each character followed by a space and increment c after every iteration.
- The loop prints characters from h through u in sequence.
- When c becomes 'v', the condition c <= c2 becomes false.
- Therefore, the output is "h i j k l m n o p q r s t u ".
#include <iostream>
using namespace std;
void alphabets(char c1, char c2)
{
// Traverse from c1 to c2
for (char c = c1; c <= c2; c++)
{
cout << c << " ";
}
}
int main()
{
char c1 = 'h', c2 = 'u';
alphabets(c1, c2);
return 0;
}
public class GFG {
// Traverse from c1 to c2
static void alphabets(char c1, char c2)
{
for (char c = c1; c <= c2; c++) {
System.out.print(c + " ");
}
}
public static void main(String[] args)
{
char c1 = 'h', c2 = 'u';
alphabets(c1, c2);
}
}
def alphabets(c1, c2):
# Traverse from c1 to c2
for c in range(ord(c1), ord(c2) + 1):
print(chr(c), end=' ')
if __name__ == '__main__':
c1 = 'h'
c2 = 'u'
alphabets(c1, c2)
using System;
class GFG {
// Traverse from c1 to c2
static void alphabets(char c1, char c2)
{
for (char c = c1; c <= c2; c++) {
Console.Write(c + " ");
}
}
static void Main()
{
char c1 = 'h', c2 = 'u';
alphabets(c1, c2);
}
}
function alphabets(c1, c2)
{
// Traverse from c1 to c2
for (let c = c1.charCodeAt(0); c <= c2.charCodeAt(0);
c++) {
process.stdout.write(String.fromCharCode(c) + "");
}
}
// Driver Code
let c1 = "h", c2 = "u";
alphabets(c1, c2);
Output
h i j k l m n o p q r s t u