165k views
2 votes
Let's write a simple markdown parser function that will take in a single line of markdown and be translated into the appropriate HTML.

1 Answer

2 votes

Answer:

Here is the answer in Java with appropriate comments for understanding

Step-by-step explanation:

import java.util.Scanner;

public class Hash {

public static void main(String[] args) {

System.out.print("Enter a string : ");

Scanner scanner = new Scanner(System. in);

String s = scanner. nextLine(); //read string

int k=s.lastIndexOf('#'),count=0;//find last occurence of # then take the next part

String s2="",s3="";

for(int i=0;i<s.length();i++)

{

if(s.charAt(i)=='#')

count++;//count the occurence of # of level of heading h1,h2,h3,....

}

for(int j=k+1;j<s.length();j++)

{

s2+=s.charAt(j);//take the remainging string after #

}

// System.out.println(k);

//System.out.println(count);

if(count<=6)//if it is valid heading

{

s3="<h"+count+">"+s2+"</h"+count+">";

System.out.println(s3);

}

else

System.out.println("Invalid header");

}

}

User Lee Hesselden
by
5.0k points