Remove whitespace from string javascript

  • To remove white space in a string in JavaScript at the starting and ending. i.e both sides of a string we have trim() method in JavaScript.
  •  By Using .trim() method we can remove white spaces at both sides of a string.
  • var str = "       instanceofjava.com        ";
    alert(str.trim());
  • Will alert a popup with instanceofjava.com.
Javascript remove last character if comma 
  • If  we want to remove last character of a string then we need to use replace method in JavaScript.
  • str = str.replace(/,\s*$/, "");
  • We can also use slice method to remove last character.
remove white spaces in javascript

Remove specific characters from a string in Javascript


  • To remove specific character from string in java script we can use

  1. var string = 'www.Instanceofjava.com'; 
  2. string.replace(/^www.+/i, ''); 'Instanceofjava.com'

C program for addition subtraction multiplication and division using switch case

  • Write a C program to simulate a simple calculator to perform arithmetic operations like a and division only on integers. Error message should be reported if any attempt is made to divide by zero    
  • C program for addition subtraction multiplication and division using switch case
  • Write a c program to perform arithmetic operations using switch case
  • C program to add subtract multiply and divide two numbers using functions
Program #1: Write a c program to perform arithmetic operations using switch case



  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. int main()
  5. {
  6.     char oper;            /* oper is an operator to be selected */
  7.     float n1, n2, result;
  8.  
  9.     printf ("Simulation of a Simple Calculator\n\n");
  10.  
  11.     printf("Enter two numbers\n");
  12.     scanf ("%f %f", &n1, &n2);
  13.  
  14.     fflush (stdin);
  15.  
  16.     printf("Enter the operator [+,-,*,/]\n");
  17.     scanf ("%c", &oper);
  18.  
  19.     switch (oper)
  20.        {
  21.         case '+': result = n1 + n2;
  22.               break;
  23.         case '-': result = n1 - n2;
  24.               break;
  25.         case '*': result = n1 * n2;
  26.               break;
  27.         case '/': result = n1 / n2;
  28.               break;
  29.         default : printf ("Error in operation\n");
  30.               break;
  31.     }
  32.  
  33.     printf ("\n%5.2f %c %5.2f= %5.2f\n", n1,oper, n2, result);
  34. getch();
  35. }


Output:

algorithm for switch case in c


  • Output
    Simulation of Simple Calculator

    Enter two numbers
    3 5
    Enter the operator [+,-,*,/]
    +

     3.00 +  5.00=  8.00
     
  • RUN2
    Simulation of Simple Calculator

    Enter two numbers
    12.75
    8.45
    Enter the operator [+,-,*,/]
    -

    12.75 -  8.45=  4.30
  • RUN3
    Simulation of Simple Calculator

    Enter two numbers
    12 12
    Enter the operator [+,-,*,/]
    *

    12.00 * 12.00= 144.00
     
  • RUN4
    Simulation of Simple Calculator

    Enter two numbers
    5
    9
    Enter the operator [+,-,*,/]
    /

     5.00 /  9.00=  0.56

Print semicolon without using semicolon in java

  • Write a program to print a semicolon without using a semicolon anywhere in the code in java.
  • Write a program to print"hello world"without using semicolon anywhere in the code.\
  • Yes we can print ";" without using semicolon in java by using printf in java.
  • Format text using printf in java here we have provided information about how to format text using printf in java.
  • In the same way we can print ";" using printf .
  • ASCII value for ";" is 59. So if we print 59 in character format it will print ;
  • System.out.printf("%C",59).
  • But in java program end of every statement we need to keep ";". To eliminate this we can use if condition.
  • Keep print statement inside if condition and it will print the ";" now.
  • Now let us see an example java program to print a semicolon without using a semicolon anywhere in the code in java.


Program #1: Write a program to print a semicolon without using a semicolon anywhere in the code in java 


  1. package instanceofjava;
  2. /**
  3.  * Print semicolon without using semicolon in java
  4.  * Print hello world without using semicolon java program code
  5.  * @author www.instanceofjava.com
  6.  */
  7. public class PrintSemiColon {
  8.     
  9. public static void main(String[] args) {
  10.     
  11.     if(System.out.printf("%C",59) != null){
  12.         
  13.     } 
  14.     
  15. }
  16. }

Output:

  1. ;


Program #1: Write a program to print hello world without using a semicolon anywhere in the code in java

  1. package instanceofjava;
  2. /**
  3.  * Print semicolon without using semicolon in java
  4.  * Print hello world without using semicolon java program code
  5.  * @author www.instanceofjava.com
  6.  */
  7. public class PrintSemiColon {
  8.     
  9. public static void main(String[] args) {
  10.     
  11.     if(System.out.printf("Hello World")!=null){
  12.         
  13.     }
  14.     
  15.     
  16. }
  17. }

Output:

  1. Hello World


print semilolon without semicolon

C programming power function example program

  • C program to find power of a number using function.
  • Write a c program to find power of a number using function recursion



 Program #1 : Write a c program to find power of a number without using function recursion

  1.  #include<stdio.h>
  2. int main(){
  3.   int power,num,i=1;
  4.   long int sum=1;
  5.   printf("Enter a number:\n ");
  6.   scanf("%d",&num);
  7.   printf("\nEnter power:\n ");
  8.   scanf("%d",&power);
  9.   while(i<=power){
  10.             sum=sum*num;
  11.             i++;
  12.   }
  13.   printf("\n%d to the power %d is: %ld",num,power,sum);
  14.   getch();
  15. }

  Output:


power of number in c



  Program #2 : Write a c program to find power of a number using function recursion


  1.  #include<stdio.h>
     
  2. void powerofnumber(int power, int num);
  3. int main(){
  4.   int power,num,i=1;
  5.   long int sum=1;
  6.   printf("Enter a number:\n ");
  7.   scanf("%d",&num);
  8.   printf("\nEnter power:\n ");
  9.   scanf("%d",&power);
  10.   powerofnumber(power,num);
  11.   return 0;
  12. }
  13.  
  14. void powerofnumber(int power, int num){
  15.      int i=1,sum=1;
  16.      while(i<=power){
  17.             sum=sum*num;
  18.             i++;
  19.   }
  20.   printf("\n%d to the power %d is: %ld",num,power,sum);
  21.  
  22. }
 

Output:


  1. Enter a number:
  2.  23
  3.  
  4. Enter power:
  5.  3
  6.  
  7. 23 to the power 3 is: 12167

C program to display characters from a to z using loop

  • C program to display characters from a to z using loop.
  • We can print all alphabets starting from A to Z using for loop.
  • Take a char variable and using for loop iterate it from A to Z.
  • Let us see an Example C program to print A to Z using for loop.



Program #1: Write a C program to print or display characters from A to Z using for loop.

  1. #include <stdio.h>
  2. int main()
  3. {
  4.     char c;
  5.  
  6.     for(c = 'A'; c <= 'Z'; ++c)
  7.       printf("%c ", c);
  8.     
  9.     return 0;
  10. }

   Output:

  1. 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

Program #2: Write a C Program to Display English Alphabets in Uppercase and Lowercase

  1. #include <stdio.h>
  2. int main()
  3. {
  4.      char c;
  5.  
  6.     printf("Enter u to display alphabets in uppercase. And enter l to display alphabets in
  7. lowercase: ");
  8.     scanf("%c", &c);
  9.  
  10.     if(c== 'U' || c== 'u')
  11.     {
  12.        for(c = 'A'; c <= 'Z'; ++c)
  13.          printf("%c ", c);
  14.     }
  15.     else if (c == 'L' || c == 'l')
  16.     {
  17.         for(c = 'a'; c <= 'z'; ++c)
  18.          printf("%c ", c);
  19.     }
  20.     else
  21.        printf("Error! You entered invalid character.");
  22.     getch();
  23. }
     

  Output:

print a to z c program

Convert string to integer c programming

  • We can convert string to integer in c Programming by using atoi() function.
  • atoi() is a library function from C. 
  • int atoi(const char *str) ;
  • Now we will write a C program to convert String format number to integer.
  • Read input from user as string.
  • Convert string to integer using atoi() function.

Program #1: Write a C program to convert String to integer in c Programming.

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int main() {
  5.    int number;
  6.    char number_string[3];
  7.  
  8.    printf("Please Enter a number : ");
  9.    scanf("%s", number_string);
  10.  
  11.    number = atoi(number_string);
  12.    printf("\n number_string : %d", number);
  13.  
  14.    getch();
  15. }


 Output:


convert String to int in c program

Select Menu