Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

C Program to Check Strings are Anagram or Not

/* C Program - Check Anagram or Not */

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str1[20],str2[20];
int len,len1,len2,i,j,found=0,not_found=0;
printf("Enter first string :");
gets(str1);
printf("Enter second string :");
gets(str2);
len1=strlen(str1);
len2=strlen(str2);
if(len1==len2)
{
len=len1;
for(i=0;i<len;i++)
{
found=0;
for(j=0;j<len;j++)
{
if(str1[i]==str2[j])
{
found=1;
break;
}
}
if(found==0)
{
not_found=1;
break;
}
}
if(not_found==1)
{
printf("Strings are not Anagram to each other");
}
else
{
printf("Strings are Anagram");
}
}
else
{
printf("Two string must have same number of character to be Anagram");
}
getch();
}

Java Program to Read and Display File

/* Java Program Example - Read and Display File's Contents */

import java.util.Scanner;
import java.io.*;

public class JavaProgram
{
    public static void main(String[] input)
    {
        String fname;
        Scanner scan = new Scanner(System.in);
        
        /* enter filename with extension to open and read its content */
        
        System.out.print("Enter File Name to Open (with extension like file.txt) : ");
        fname = scan.nextLine();
        
        /* this will reference only one line at a time */
        
        String line = null;
        try
        {
            /* FileReader reads text files in the default encoding */
            FileReader fileReader = new FileReader(fname);
            
            /* always wrap the FileReader in BufferedReader */
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            
            while((line = bufferedReader.readLine()) != null)
            {
                System.out.println(line);
            }
            
            /* always close the file after use */
            bufferedReader.close();
        }
        catch(IOException ex)
        {
            System.out.println("Error reading file named '" + fname + "'");
        }
    }
}