SynCfusion Interview Question
Applications DevelopersTeam: 1
Country: India
Interview Type: Written Test
///// modifications
// 1.> Have to print in multiples of 3 only when string length is greater or equal than 3
// only3 i should increase by 3 each time and no if condition required in only3
public static void funny(String str){
int length=str.length();
if(length >= 3 &&.length%3==0){ // String should be greater or equal than 3 //characters
only3(str);
} else {
System.out.println(str);
}
}
public static void only3(String str){
for(int i = 2; i<str.length(); i=i+3){ // need to print in case multiple of 3 from 3rd //character, no need of having if else condition
System.out.print(str.charAt(i));
}
}
static inline bool
divisible_by_three
( unsigned value )
{
return 0 == value % 3;
}
void
threeLength
( char * string, char * buffer )
{
if( nullptr == string || nullptr == buffer){
throw std::invalid_argument("Neither argument can be null");
}
unsigned length = strlen( string );
if( !divisible_by_three(length) ){
sprintf(buffer, "%s", string);
}
else {
int j = 0;
for( int i = 2; i < length; i += 3){
buffer[j++] = string[i];
}
buffer[j] = '\0';
}
}
int
main
( int argc, char ** argv )
{
char buffer[50];
threeLength("elephants", buffer);
std::cout << buffer << std::endl;
}
Without the cost of finding length. You can continue to find the 3rd character in the string and find the next character and find the current character + 3...all in one iteration.
If next character is null...then the length is multiples of 3...print collected characters
If current char + 3 exists contine
If next char is not null, but current + 3is null then it is not multiple of 3... break and print entire string.
In Java :
If string length is not a multiple of given number(c=3), return string as is
otherwise, step through string starting at index c-1, incrementing index by c and
get character at those indices and construct a return value.
public String printPositional(String str, int c) {
if (str.length() % c != 0)
return str;
StringBuilder sb = new StringBuilder();
for(int i=c-1;i<str.length();i+=c){
sb.append(str.charAt(i));
}
return sb.toString();
}
import java.io.*;
public class str {
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the string");
String nu=br.readLine();
int ch=nu.length();
for(int i=0;i<ch;i++){
System.out.print(nu.charAt(i));
}
for(int k=2;k<ch;)
{
char ab=nu.charAt(k);
System.out.print(ab);
k=k+3;
}
}
}
Don't get complicated its just a simple.
using System;
using System.Linq;
namespace SyncTest
{
class Program
{
static void Main(string[] args)
{
string input;
input = Console.ReadLine();
if(input.Length%3==0)
{
Console.WriteLine(" Enter an input string : ");
Console.WriteLine(input);
}
else
{
int j;
for( j=0; j<input.Length; j++)
{
Console.WriteLine(input.ElementAtOrDefault(j + 2));
j=j + 2;
}
}
Console.ReadKey();
}
}
}
using System;
using System.Linq;
namespace SyncTest
{
class Program
{
static void Main(string[] args)
{
string input;
input = Console.ReadLine();
if(input.Length%3==0)
{
Console.WriteLine(" Enter an input string : ");
Console.WriteLine(input);
}
else
{
int j;
for( j=0; j<input.Length; j++)
{
Console.WriteLine(input.ElementAtOrDefault(j + 2));
j=j + 2;
}
}
Console.ReadKey();
}
}
}
class Program
{
static void Main(string[] args)
{
string s = "Elephants";
char[] sen = s.ToCharArray(); // converting string into char array
int num = 3;
int l = sen.Length; // l contains the length of sen
if(l%2!=0)
{
for(int i=num-1;i<sen.Length;i=i+3)
{
Console.Write(sen[i]);
}
}
Console.ReadLine();
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class StringMultiple {
public static void main(String[] args) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the String");
String str=br.readLine();
int a=str.length();
System.out.println(a);
if(a%3==0)
{
char[] strArray=str.toCharArray();
for(int i=2;i<a;)
{
System.out.println(strArray[i]);
i=i+3;
}
}
else
{
System.out.println(str);
}
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class StringMultiple {
public static void main(String[] args) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the String");
String str=br.readLine();
int a=str.length();
System.out.println(a);
if(a%3==0)
{
char[] strArray=str.toCharArray();
for(int i=2;i<a;)
{
System.out.println(strArray[i]);
i=i+3;
}
}
else
{
System.out.println(str);
}
}
}
#include<stdio.h>
- Avi October 19, 2012int main()
{
int i=0,j;
char a[] = "elephants";
i= (sizeof(a)-1);
if( (i % 3) != 0)
{
printf("%s \n",a);
return 0;
}
for(j=2;j<i;j=j+3)
printf("%c ",a[j]);
printf("\n");
return 0;
}