Skip to main content

Entering a string and printing it in JAVA.



import java.util.*;
class InputString
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter your string");
String s=sc.nextLine();
System.out.println();
System.out.print("Printing your String");
System.out.println();
System.out.print(s);
}
}



Comments

Popular posts from this blog

Check if two strings are equal.

In this post we will see how to check if two strings are equal in C.For this we will use strcmp function. It compares two strings  lexicographically i.e character by character, if the two character matches moves to next character and compares it. It returns 0 if both strings are equal. #include<stdio.h> #include<string.h> int main() { char str[25]; printf("Enter string 1.\n"); gets(str); char str1[25]; printf("\nEnter string 2.\n"); gets(str1); int i = strcmp(str, str1); if(i==0) { printf("Equal.\n"); } else { printf("Not Equal.\n"); } return 0; }

Outcomes of a Dice || Generating Random numbers in Java

import java.util.*; class GenerateRandomNumber { public static void main(String args[]) { int i=0; Random r=new Random(); Scanner sc=new Scanner(System.in); System.out.println("How many random numbers ,do you want to generate?"); int value=sc.nextInt(); System.out.println(); System.out.println("Generating Numbers"); for(i=0;i<value;i++) { int n=r.nextInt(6) + 1; System.out.println(n); } } } /*In the loop it is written: int n=r.nextInt(6) + 1; Here 6 inside the parentesis is the maximum value of random number,since here we are considering a dice so I have set that to 6. And the +1 indicates the minimum possible number generated.You have to include import java.util.*; library to use Random(). */

Find number of vowels in a string.

#include<stdio.h> int main() { char str[25]; printf("Enter string.\n"); gets(str); int i = 0; int count = 0; while(str[i] != '\0') { if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') { count++; } i++; } printf("\nNo. of vowels is %d",count); return 0; }