Skip to content

Instantly share code, notes, and snippets.

@gauravvichare
Created April 14, 2013 14:44
Show Gist options
  • Save gauravvichare/5382988 to your computer and use it in GitHub Desktop.
Save gauravvichare/5382988 to your computer and use it in GitHub Desktop.
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc. Th…
import java.util.Scanner;
public class convert {
public static void main(String [] args){
int i,j,count;
String[] ab;
ab = new String[10000000];
Scanner in=new Scanner (System.in);
count=in.nextInt();
for(i=0;i<count;i++){
ab[i]=in.next();
}
for(i=0;i<count;i++){
String final1="";
String final2="";
String f="",s="";
if(ab[i].charAt(0)=='R'){
int indexoflastc=ab[i].lastIndexOf("C");
s=ab[i].substring(indexoflastc+1, ab[i].length());
int num=Integer.parseInt(s);
final1=co1(num)+ab[i].substring(ab[i].lastIndexOf("R")+1,ab[i].lastIndexOf("C"));
System.out.println(final1);
}
else{
for(j=0;(int)ab[i].charAt(j)>58;j++);
f=ab[i].substring(j, ab[i].length());
final2="R"+f+"C"+co(ab[i].substring(0, j));
System.out.println(final2);
}
}
}
public static int co(String a){
int d=0,length,i,cnt;
length=a.length();
cnt=length-1;
for(i=0;i<length;i++){
d=(int) (d+((int)a.charAt(i)-64)*Math.pow(26, cnt));
cnt--;
}
return d;
}
public static String co1(int a){
int c;
String d="";
a=a-1;
while(a>=0){
c=a-(int)(a/26)*26;
d=(char)(c+65)+d;
a=a/26-1;
}
return d;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment