Skip to main content

C variable format to Java Variable String format : snakeCase to java convention and vice versa

How to convert snake case format containing underscore to java variable format and vice versa?
 Usually c variable contains underscore between words but java contains firstletter is lowercase and other first letter in upper case.

This is very famous test program usually asked in online test.



example :
 input = bb_cann_fb_hnh
output = bbCannFbHnh

and vice versa.

public class VariableFormatter {
public static void main( String[] args )
    {
        System.out.println(modifyVariableString("bb_cann_fb_hnh"));
        System.out.println(modifyVariableString("bbCannFb"));
     
    }
 
 
   private static String modifyVariableString(String s) {

   String output;

//check if it is c variable

   if(s.contains("_")){
   output= javaFormatter(s);
   }else{
   output= cFormatter(s);
   }
return output;
}


private static String cFormatter(String string) {
   string= string.replaceAll("([A-Z])", "_$1").toLowerCase();
return string;
}


static String javaFormatter(String s){
  String[] elements= s.split("_");

  StringBuilder sb = new StringBuilder(elements[0]);
    for(int i=1;i<elements.length;i++){
    elements[i]=elements[i].substring(0, 1).toUpperCase()+elements[i].substring(1);
    sb.append(elements[i]);
    }
   
    String output=sb.toString();
   
    return output;
    }
}

Comments

Popular posts from this blog

Introduction to Reactive Programming in Java with Project Reactor

Introduction to Reactive Programming in Java with Project Reactor we will go through below: 1. Introduction to Reactive Programming What is reactive programming? Key principles of reactive systems: Responsive : Systems should respond in a timely manner. Resilient : Systems should be fault-tolerant. Elastic : Systems should scale as needed. Message-driven : Systems should use asynchronous messaging. Comparison between imperative programming and reactive programming. 2. Understanding Reactive Streams Publisher , Subscriber , Subscription , and Processor interfaces. The four key signals: onNext() , onComplete() , onError() , and onSubscribe() . Backpressure handling in reactive systems. 3. Introduction to Project Reactor What is Project Reactor? Key classes: Mono and Flux . Mono : Represents 0 or 1 item. Flux : Represents 0 to N items. Non-blocking nature and how it helps in building scalable systems. 4. Building a Reactive Application with Project Reactor Demonstrating how to use Mono ...

Camunda Spring Boot with Shared Engine with War file

This is the only one website (may be first) to show an example to create working war file for camunda shared engine using Spring Boot. We analysed the problem using pure spring boot "starter" dependencies that it contains camunda engine plus tomcat, so even when you remove tomcat using either <provided> or <exclude>, it continues to run on tomcat as its already with in camunda engine. And when you try to create war file and paste in into shared camunda engine outside then that war will no work. This is because that war you created containing camunda engine and a tomcat inside it and when you paste into another outside camunda engine, it gets confused to which camunda to use. Hence I worked on this and brought a mix version of spring and spring boot implementation of camunda. So here you go....and thanks me later :) Here is the pom file -------------------------------------------------------------------------------------------- <?xml version=...

Java Signature Certificate

Signature A valid digital signature gives a recipient reason to believe that the message was created by a known sender ( authentication ), that the sender cannot deny having sent the message ( non-repudiation ), and that the message was not altered in transit ( integrity )   Original-data.getByte() + private key à signature All are in byte array As signature has to be passed through network, we convert into string but we cant use just string of original byte array because of security. Hence we do manipulation of byte array and convert into a different string. We shoud not send new String(byte [] signature) , we should send   new String(Base64.encode(byte[] signature)) Verification: Client send (new data + signature) object{ User name: Ram Id : 678 Signature : ZTngSmnSpmWIj40r5TQ1hmec0UbfJLSCRSxbVBxCwchcFu6A8RS+O9BUFgG7U+UozVlrO5xGl9tARHxcIK4y2x/UHvhfYu74SOq22XgdGNuPMGQ560pUpiSkXspfGuFh9xHqovNGs7MQvWyESgurqehdsFD18sXV0z7gnqqFm78=...