Interview Question
Team: Subex
Country: India
Interview Type: Written Test
let the text file name be text.dat.
void transp_matrix(FILE* fp,int rows,int col)
{
int j=0;
char **p;
p=malloc(rows*sizeof(char*));
for(int i=0;i<rows;i++)
{
p[i]=malloc(col*sizeof(char));
while(int ch=fgetc(fp) && ch!='\n')
{
p[i][j]=ch;
j++;
}
}
for(i=0;i<rows;i++)
for(j=0;j<col;j++)
{
p[j][i]=p[i][j];
}
}
String s1="asd";
String s2="bte";
String s3="cu";
char[] c1=s1.toCharArray();
char[] c2=s2.toCharArray();
char[] c3=s3.toCharArray();
Object[] o1={c1[0],c2[0],c3[0]};
Object[] o2={c1[1],c2[1],c3[1]};
Object[] o3={c1[2],c2[2]};
for(Object p1:o1)
{
System.out.print(p1);
}
System.out.println();
for(Object p2:o2)
{
System.out.print(p2);
}
System.out.println();
for(Object p3:o3)
{
System.out.print(p3);
}
very nice dude,,,,really thank u..any one new 2 ths ..just copy the code as i mentioned below and paste,run it in this. browxy.com
import java.io.*;
public class Check
{
public static void main(String args[])
{
String s1="asd";
String s2="bte";
String s3="cu";
char[] c1=s1.toCharArray();
char[] c2=s2.toCharArray();
char[] c3=s3.toCharArray();
Object[] o1={c1[0],c2[0],c3[0]};
Object[] o2={c1[1],c2[1],c3[1]};
Object[] o3={c1[2],c2[2]};
for(Object p1:o1)
{
System.out.print(p1);
}
System.out.println();
for(Object p2:o2)
{
System.out.print(p2);
}
System.out.println();
for(Object p3:o3)
{
System.out.print(p3);
}}
}
String s1="asd";
String s2="bte";
String s3="cu";
char[] c1=s1.toCharArray();
char[] c2=s2.toCharArray();
char[] c3=s3.toCharArray();
int size = Math.MAX(c1.length,c2.length,c3.length)
for(int i=0;i<size;i++){
if(c1.length>i){
System.out.print(c1[i]);
}
if(c2.length>i){
System.out.print(c2[i]);
}
if(c3.length>i){
System.out.print(c3[i]);
}
System.out.println("\n");
}
I have written a program which reads from a file ( size does not matter - tested with 25mb file) and then transposes the whole strings in that file .
input 1) hello
dello
rello
input 2) skydrive
videodrive
store
For input 2 I add space where required and then print ,
$ = space
skydrive$
videodrive
store$$$$
After adding the space I transpose the strings in the file.
#####################################################################
package com.CareerCup;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
public class TransposeStringInFile {
public static void main (String string[]) throws Exception
{
int coloum=0;
int row=0;
FileReader freader=null;
BufferedReader bufferReader=null;
//we read the file line by line and put each line of the file into this arrary as one string
ArrayList <String> LinesInFile = new ArrayList<String>();
try{
File file = new File("C:\\urlFile1.txt");
freader = new FileReader(file);
bufferReader = new BufferedReader(freader);
String temStr;
int strLen;
while( (temStr=bufferReader.readLine())!= null)
{
//we try to find the longest line so that our charArray will have that long coloum
strLen = temStr.length();
if(strLen > coloum)
{
coloum=temStr.length();
}
//add each line in the file to the arrayList
LinesInFile.add(temStr);
}
//the rows are nothing but the number of lines in a file
row = LinesInFile.size();
//we have calculated the max length of a line ( string) in the file so the charArray will be with that dimensiton = row X colum matrix
//where row is number of lines in the file . Coloum is longest line in the file .
char [][] charactersInFile = new char[row][coloum];
for(int i=0; i< row; i++)
{
char tempCh[] = LinesInFile.get(i).toCharArray();
int ListCharLen= tempCh.length;
if( ListCharLen < coloum)
{
//this method is used to padd the space when its not a square matrix if the input is like
// one
// seven
// two
//Then we create an arry and padd the space where reqired here ---> $ = space
// one$$
// seven
// two$$
paddSpace(ListCharLen , tempCh ,charactersInFile[i] ,coloum );
}
else
charactersInFile[i]=tempCh;
}
for(int i=0;i<coloum;i++)
{
for(int j=0;j<row;j++)
{
System.out.print(charactersInFile[j][i]);
}
System.out.print("\n");
}
}
finally
{
bufferReader.close();
freader.close();
}
}
//this method is used to padd the space when its not a square matrix if the input is like
// one
// seven
// two
//Then we create an arry and padd the space where reqired here ---> $ = space
// one$$
// seven
// two$$
static void paddSpace(int ListCharLen , char tempCh[] , char OriginalCharactersInFile[] , int maxLen )
{
int i=0;
for( ; i<ListCharLen;i++)
{
OriginalCharactersInFile[i]= tempCh[i];
}
//adding space for the left length
int padd = maxLen -ListCharLen;
while(padd != 0)
{
OriginalCharactersInFile[i]= ' ';
i++;
padd--;
}
}
}
public class FileStringTranspose {
public static void main(String[] args) throws IOException {
File f = new File("Data.txt");
BufferedReader br = new BufferedReader(new FileReader(f));
List<Integer> sizeList = new LinkedList<Integer>();
List<String> lines = new LinkedList<String>();
String line = null;
//Read from the file and add line to the List.
while ((line = br.readLine()) != null) {
lines.add(line);
sizeList.add(line.length());
}
//Sort the sizeList to get the max length of a line at the last index.
Collections.sort(sizeList);
//Get the value at the last index of the Size List
int largestCharPosition = sizeList.get(sizeList.size() - 1);
int j = 0;
while(j < largestCharPosition) {
for(int i = 0; i < lines.size(); i++) {
int length = lines.get(i).length();
if(length > j) {
System.out.print(lines.get(i).charAt(j));
}
}
System.out.println();
j++;
}
}
}
- coding.arya July 27, 2013