Q:

Program to separate the individual characters from a string

belongs to collection: String Programs

0

Explanation

In this program, we need to separate each character from the string.

CHARACTERS  

C   H   A   R   A   C   T   E   R   S  

In computer science, collection of characters including spaces is called as string. To separate an individual character from the string, individual character are accessed through its index.

Algorithm

  1. Define a string.
  2. Define a for-loop in which loop variable will start from 0 and end at length of the string.
  3. To separate each character, we will print the character present at every index.
  4. To visualize this, we have separated each character by a space.

Input:

string = "characters"  

Output:

Individual characters from given string: characters

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python

string = "characters";  
   
#Displays individual characters from given string  
print("Individual characters from given string:");  
   
#Iterate through the string and display individual character  
for i in range(0, len(string)):  
    print(string[i], end="  ");  

 

Output:

Individual characters from given string: 
c  h  a  r  a  c  t  e  r  s  

 

C

#include <stdio.h>  
#include <string.h>  
   
int main()  
{  
    char string[] = "characters";  
          
    //Displays individual characters from given string  
    printf("Individual characters from given string:\n");  
      
    //Iterate through the string and display individual character  
    for(int i = 0; i < strlen(string); i++){  
        printf("%c ", string[i]);  
    }  
          
    return 0;  
}

  

Output:

Individual characters from given string:
c  h  a  r  a  c  t  e  r  s  

 

JAVA

public class IndividualCharacters  
{  
    public static void main(String[] args) {  
        String string = "characters";  
          
        //Displays individual characters from given string  
        System.out.println("Individual characters from given string:");  
          
        //Iterate through the string and display individual character  
        for(int i = 0; i < string.length(); i++){  
            System.out.print(string.charAt(i) + "  ");  
        }  
    }  
}  

 

Output:

Individual characters from given string:
c  h  a  r  a  c  t  e  r  s  

 

C#

using System;  
                      
public class IndividualCharacters  
{  
    public static void Main()  
    {  
        String string1 = "characters";  
          
        //Displays individual characters from given string  
        Console.WriteLine("Individual characters from given string:");  
          
        //Iterate through the string and display individual character  
        for(int i = 0; i < string1.Length; i++){  
            Console.Write(string1[i] + "  ");  
        }  
    }  
}  

 

Output:

Individual characters from given string:
c  h  a  r  a  c  t  e  r  s  

 

PHP

<!DOCTYPE html>  
<html>  
<body>  
<?php  
$string = "characters";  
   
//Displays individual characters from given string  
print("Individual characters from given string: <br>");  
   
//Iterate through the string and display individual character  
for($i = 0; $i < strlen($string); $i++){  
    print($string[$i] . "  ");  
}  
?>  
</body>  
</html>   

 

Output:

Individual characters from given string:
c  h  a  r  a  c  t  e  r  s  

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


Program to swap two string variables without using... >>
<< Program to print smallest and biggest possible pal...