Showing posts with label c language. Show all posts
Showing posts with label c language. Show all posts
on Sunday, 14 April 2013

/* Count number of 1 bits */
#include<stdio.h>

int counter(int data){
int count=0;
int loop_counter=1;
int test=1;
while(loop_counter<=16){
if(data&test)
count++;
test=test<<1;
loop_counter++;
}

return count;
}


void main(){
int data=32768; // can take any data in input
printf(" No. of 1 bits = %d",counter(data));

}

/* Logic of toLowerCase() in c language */

#include<stdio.h>


void toUpperCase(char *source){
int index=0;

while(source[index]!='\0'){

char ch=source[index];
// first check it is valid alphabet or not

if(!(ch&64)){
index++;
continue;
}


// is already lowerCase
if(ch&32){
index++;
continue;
}

switch(ch){
case '64': case '91': case '92': case '93': case '94': case '95':
// case '96': case '123': case '124': case '125': case '126': case '127':
index++;
break;

default:
source[index]=ch|32;  // now actual logic to  convert into uppercase
index++;

}



}

}



void main(){
 char source[]="PALASH CHANDEL"; // can supply any input
 toUpperCase(source);
 printf("%s",source);


}
on Saturday, 13 April 2013

/* Logic of toUpperCase() in c language */

#include<stdio.h>


void toUpperCase(char *source){
int index=0;

while(source[index]!='\0'){

char ch=source[index];
// first check it is valid alphabet or not

if(!(ch&64)){
index++;
continue;
}


// is already upperCase
if(!(ch&32)){
index++;
continue;
}

switch(ch){
case '64': case '91': case '92': case '93': case '94': case '95':
// case '96': case '123': case '124': case '125': case '126': case '127':
index++;
break;

default:
source[index]=ch&95;  // now actual logic to  convert into uppercase
index++;

}



}

}



void main(){
 char source[]="123abcAgc"; // can supply any input
 toUpperCase(source);
 printf("%s",source);


}

/* Remove white space with sign */


#include<stdio.h>
void remove_white_space(char *source) {  // including first sign(+/-) if exist
int iffc=0; //iffc=index_find_first_character after first sign
int ic=0; // index copy

/* find first character */
while(source[iffc]!='\0'){

if(source[iffc]==' '){ // seek for space
iffc++;
continue;
}

if(source[iffc]=='+' || source[iffc]=='-'){
iffc++;
break;
}

break;
}

if(iffc==0) // no whitespace found
return;


/* copy string part onto white space */


while(source[iffc]!='\0'){
source[ic]=source[iffc];
printf("%c",source[ic]);
ic++;
iffc++;

}
source[ic]='\0'; // insert null at the end of string

 }



void main(){
char source[]="palash";  // can supply any input
printf("%s",source);
remove_white_space(source);
printf("\n%s",source);


}

 /* parseint() c version */


#include<stdio.h>
void remove_white_space(char *source) {  // including first sign(+/-) if exist
int iffc=0; //iffc=index_find_first_character after first sign
int ic=0; // index copy

/* find first character */
while(source[iffc]!='\0'){

if(source[iffc]==' '){ // seek for space
iffc++;
continue;
}

if(source[iffc]=='+' || source[iffc]=='-'){
iffc++;
break;
}

break;
}

if(iffc==0) // no whitespace found
return;


/* copy string part onto white space */


while(source[iffc]!='\0'){
source[ic]=source[iffc];
ic++;
iffc++;

}
source[ic]='\0'; // insert null at the end of string

 }


 int parseInt(char *source){
  remove_white_space(source);
  int index=0;
  int result=0;

while(source[index]!='\0'){

switch(source[index]){

case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
result=result*10+(source[index++]-48);
break;

default:
return result;

}


}
return result;

 }


void main(){
char source[]="456"; // can supply any input ,,
printf("%d",parseInt(source));


}

/*  Find absolute value */
#include<stdio.h>
int abs(int number){
if(number & 65536)
return (~number+1);
return number;
}

void main(){
printf("%d",abs(5)); // can pass any input,,
}

/* Check is number even or odd*/

#include<stdio.h>
void main(){
int a;
printf("Enter the value : ");
scanf("%d",&a);
if(a&1)
printf("\n Number is odd");
else
printf("\n Number is even ");
}