0% found this document useful (0 votes)
69 views5 pages

Round 2

The document contains code snippets for various C functions that perform operations like checking for whitespace characters, changing whitespace to periods in a string, evaluating number guesses, determining if a year is a leap year, counting positive numbers in an array, correcting degree measurements, checking if all numbers in an array are positive, finding the last positive number in an array, checking if a number is binary, swapping the signs of numbers in an array, checking if a number is divisible by 3, comparing the case of characters, converting a string to uppercase, and finding the index of the first uppercase or lowercase 'a' in a string.

Uploaded by

Lamore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views5 pages

Round 2

The document contains code snippets for various C functions that perform operations like checking for whitespace characters, changing whitespace to periods in a string, evaluating number guesses, determining if a year is a leap year, counting positive numbers in an array, correcting degree measurements, checking if all numbers in an array are positive, finding the last positive number in an array, checking if a number is binary, swapping the signs of numbers in an array, checking if a number is divisible by 3, comparing the case of characters, converting a string to uppercase, and finding the index of the first uppercase or lowercase 'a' in a string.

Uploaded by

Lamore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <stdio.

h>
#include <ctype.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <string.h>

*************
int is_white(const char c){
//char c=[20];
if(c==' ' || c=='\t' || c=='\n'){
return 1;
}
else{
return 0;
}
}

int main(){
printf("%d %d\n", is_white('#'), is_white(' '));
}

**************
void change_whites(char string []){
for(int i=0; i<=strlen(string); i++){
if(string[i]==' ' || string[i]=='\n'){
string[i]='.';
}
}
}

int main(){
char str[] = "Hello world!";
change_whites(str);
printf("%s\n", str);
}

***********
int guess_eval(const int guess, const int my_number){
if(guess==my_number){
return 1;
}
else{
if(guess>my_number){
return 0;
}
else{
return 2;
}
}
}

int main(){
printf("%d %d %d\n", guess_eval(34, 22), guess_eval(22, 34), guess_eval(34,
34));
}

*************
int leap_year(const int year){
if(year<1 || year>4443){
return -1;
}
else{
if(((year%400)==0 && (year%4)==0 && (year%100)==0) || ((year%4)==0 && (year
%100)!=0)){
return 1;
}
else{
return 0;
}
}
}

int main(){
printf("%d %d %d\n", leap_year(4000), leap_year(3000), leap_year(3004));
}

***********
int count_positives(const int size, const int array[]){
int count = 0;
if(array!=NULL){
for(int i=0; i<size; i++){
if(array[i]>0){
count++;
}
}
return count;
}
else{
return -1;
}
}

int main(){
const int array[]={-1,-2,0,-3,0,7};
printf("%d\n", count_positives(6, array));
}

*************
int direction_correction(const int degree){
if(degree<0 || (degree%90)!=0){
return -1;
}
int degree2=degree;
while (degree2>=360){
degree2-=360;
}
return degree2;
}

int main(){
printf("%d\n", direction_correction(630));
}
*************
int all_positives(const int size, const int array[]){
int count = 0;
if(array!=NULL){
for(int i=0; i<size; i++){
if(array[i]<=0){
count++;
}
}
}
else{
return -1;
}

if(count==0){
return 1;
}
else{
return 0;
}
}

int main(){
const int array[]={1,2,0,3,4,0};
printf("%d\n", all_positives(6, array));
}

**************
int last_positive(const int size, const int array[])
{ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
int x = 0;
if(array==NULL){
return -1;
}
else{
for(int i=0; i<size; i++){
if(array[i]>0){
x = array[i];
}
}
return x;
}
}

int main(){
const int array[]={-1,0,-6,-2};
printf("%d\n", last_positive(4, array));
}

**************
int binary_num(const int num){
if(num<-1000 || num>1000){
return -1;
}
else{
if(num==1 || num==0){
return 1;
}
else{
return 0;
}
}
}

int main(){
//const int array[]={-1,0,-6,-2};
printf("%d\n", binary_num(1));
}

**************
void swap_sign(const int size, int array[]){
if(array!=NULL){
for(int i=0; i<size; i++){
array[i]=array[i]*(-1);
}
}
}

int main(){
int array[]={1,2,0,-3,4,0};
swap_sign(6, array);
for(int i=0; i<6; i++){
printf("%d ", array[i]);
}
printf("\n");
}

**************
int div_by_3(const int num){
if(num%3=0){
return 1;
}
else{
return 0;
}
}

int main(){
printf();
}

***************
int same_case(const char a, const char b){
if((isupper(a) && isupper(b)) || (islower(a) && islower(b))){
return 1;
}
else{
if((isupper(a) && islower(b)) || (isupper(b) && islower(a))){
return 0;
}
else{
return -1;
}
}
}
int main(){
printf("%d\n", same_case('D', 'C'));
}

***************
void string_to_upper(char string[]){
for(int i=0; i<=strlen(string); i++){
if(islower(string[i]) && string!=NULL){
string[i] = tolower(string[i]);
}
}
}

***************
int find_first_A(const char string[]){
int i=0;
if(string==NULL){
return -1;
}
while(string[i]!='a' && string[i]!='A' && string[i]!='\0'){
i++;
}
if(string[i]=='\0'){
return -1;
}
return i;
}

int main()
{
printf("%d\n", find_first_A("cbbbbbbbbba"));
}

You might also like