Sei sulla pagina 1di 4

Title: Basic of Programming-Assignment 1 TCS Reference No: CT20110402153

1. Search for a name Write a program to accept an array of names and a name and check whether the name is present in the array. Return the count of occurrence. Use the following array as input {Dave, Ann, George, Sam, Ted, Gag, Saj, Agati, Mary, Sam, Ayan, Dev, Kity, Meery, Smith, Johnson, Bill, Williams, Jones, Brown, Davis, Miller, Wilson, Moore, Taylor, Anderson, Thomas, Jackson}

import java.io.*; import java.util.StringTokenizer; public class SearchNameProgram { public static void main(String args[])throws Exception { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println(".........................WELCOME TO THE NAME FINDING PROGRAM......................."); System.out.println("enter names by using space inbetween:"); String arrNames=br.readLine(); StringTokenizer st1=new StringTokenizer(arrNames); System.out.println("enter the name to be searched:"); String name=br.readLine(); int countNoOccurance=0; while(st1.hasMoreTokens()) {

if(name.equalsIgnoreCase(st1.nextToken())) { countNoOccurance++; } } System.out.println("THE NAME "+name+" HAS APPEARED "+countNoOccurance+" TIMES"); } }

1.

Search for a name Write a program to accept an array of names and a name and check whether the name is present in the array. Return the count of occurrence. Use the following array as input {Dave, Ann, George, Sam, Ted, Gag, Saj, Agati, Mary, Sam, Ayan, Dev, Kity, Meery, Smith, Johnson, Bill, Williams, Jones, Brown, Davis, Miller, Wilson, Moore, Taylor, Anderson, Thomas, Jackson}

Ans. import java.io.*; import java.util.StringTokenizer; public class namefinding { public static void main(String args[])throws Exception { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println(".........................WELCOME TO THE NAME FINDING PROGRAM......................."); System.out.println("enter names by using space inbetween:"); String arrNames=br.readLine(); StringTokenizer st1=new StringTokenizer(arrNames); System.out.println("enter the name to be searched:"); String name=br.readLine(); int countNoOccurance=0;

2.

while(st1.hasMoreTokens()) { if(name.equalsIgnoreCase(st1.nextToken())) { countNoOccurance++; } } System.out.println("THE NAME "+name+" HAS APPEARED "+countNoOccurance+" TIMES"); } } Search all the files which contain a particular string, say "include" within a folder.

Ans. grep include ./* grep searches the named input file for lines containing a match to given pattern ./* in current directory(.), all files(*) 3. Rename all the files within a folder with suffix "Unix_" i.e. suppose a folder has two files a.txt and b.pdf than they both should be renamed from a single command to Unix_a.txt and Unix_b.pdf.

Ans. for f in *.*; do mv $f Unix_$f; done *.* means all files $ indicates read the value of variable mv $_ Unix_$i will rename all file as prefix with Unix_ and filename. 4. Rename all files within a folder with the first word of their content(remember all the files should be text files. For example if a.txt contains "Unix is an OS" in its first line then a.txt should be renamed to Unix.txt.

Ans. for f in *.txt; do d="$(head -1 "$f") ;x=$(cut -d " " -f1 $f) ; mv $f $x; done 5. Suppose you have a C project in a folder called "project", it contains .c and .h files,it also contains some other .txt files and .pdf files. Write a Linux command that will count the number of lines of your text files. That means total line count of every file. (remember you have to count the lines in.txt files only).

Ans. wc l *.txt wc indicate word count -l indicate count lines

*.txt indicates all text files 6. Rename all files which contain the sub-string 'foo', replacing it with 'bar' within a given folder.

Ans. for i in ./*foo*;do mv -- "$i" "${i//foo/bar}";done 7. Show the most commonly used commands from "history". [hint: remember the history command, use cut, and sort it]

Ans. history|sort|cut d-10 n

Potrebbero piacerti anche