sudheerjay
BAN USERyou just need to implement the atoi() function from c library.
here is the hint:
1) You set the pointer at the start of the char array.
2) And then inside for loop you go over each character and multiply by 10 and add the character by subtracting by 0, and store it in the result.
something like:
/*
int res=0;
for (int i = 0; str[i] != '\0'; ++i)
res = res*10 + str[i] - '0';
*/
And if you will try with "26745". the number would be 26745 as well.
and if you need to check for signed values like "-26745"
just check if charArray[0] is ' - '. if so, store it in a variable(eg: int sign= -1 ;)
then iterate the loop from i=1 and return sign*res;
which will return -26745 for "-26745".
@Keshav: My Bad, care to check now..??
- sudheerjay June 07, 2014/*Using StringBuffer we can easily do this in java */
import java.util.Scanner;
public class WordReverse {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String inp = in.nextLine();
String[] inpArray =inp.split(" ");
StringBuilder sb = new StringBuilder();
for(int i=inpArray.length-1;i>=0;i--){
sb.append(" ").append(inpArray[i]);
}
System.out.println(sb.reverse());
}
/*A Simple Java Code would look like this: */
import java.util.Scanner;
public class SlidingWindow {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("enter number of elements:");
int n = in.nextInt();
int a[] = new int[n + 1];
int temp[] = new int[n + 1];
System.out.println("enter " + n + " elements:");
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
System.out.println("enter the window size: ");
int w = in.nextInt();
for (int i = 0; i < (a.length - w); i++) {
for (int j = i; j < w + i; j++) {
System.out.print(a[j] + " ");
temp[j] = a[j];
continue;
}
System.out.println("Minimum is: " + findMin(temp));
System.out.println("");
}
}
public static int findMin(int[] a) {
int len = a.length;
int val = a[0];
for (int i = 1; i < len; i++) {
if (val < a[i]) {
} else {
val = a[i];
}
}
return val;
}
}
this is my code and i'm new to java but not to logics :P .. please check and reply..
- sudheerjay April 10, 2014chesk BSS section in the below link:
"uninitialized data starts at the end of the data segment and contains all global variables and static variables that are initialized to zero or do not have explicit initialization in source code."
for negative numbers you just need to check the charArray[0]. if it is ' - ' then initialize previously defined variable to -1 and multiply with the result before returning the result.
- sudheerjay June 19, 2014