Wednesday, May 26, 2021

, , , , , ,

toupper() function in c with exmaple program

 toupper() function in c programming is used to convert lowercase alphabets to uppercase letters.

When a programmer passed a lowercase alphabet to toupper() function then it converts it to uppercase.

Or when a uppercase alphabet is passed then function returns same value.

Syntax for toupper() function in c :-

int toupper(int ch);

Example Program for toupper() in c :- 

Let's go with an simple s example program to convert lower case alphabet to uppercase using toupper() function.


#include <ctype.h> 

int main() 
{ 
    char ch; 

    // letter to convert to uppercase
    ch = 'a'; 

    printf("%c in uppercase is represented as %c", 
        ch, toupper(ch)); 

    return 0; 
} 


Note :- Remember toupper() in c only takes one character at a time.

So if you want to convert a hole word then use while loop for it

, , , , ,

Example program for tolower() function in C

 Well as it's name says tolower() function in C programming is used to converts a uppercase alphabet to an lowercase alphabet.

Syntax for tolower() in C :-

int tolower(int ch);

Parameter:  tolower() in C use only one parameter "ch" which is a passed character to be converted to lowercase.

Return Value: this function return converted lowercase character.

Example program for tolower() function in C

 

// C program to demonstrate
// example of tolower() function.
  
#include <ctype.h>
#include <stdio.h>
  
int main()
{
  
    // Character to be converted to lowercase
    char ch = 'G';
  
    // convert ch to lowercase using toLower()
    printf("%c in lowercase is represented as = %c", ch, tolower(ch));
  
    return 0;
}

OUTPUT :- 

tolower() in C
 tolower() function in C



Saturday, May 22, 2021

, , , , ,

strcoll() function in c with example program

strcoll() in c is a library function use to compares string str1 to str2 and result will depends on  result is the LC_COLLATE setting of the location.

Syntax for strcoll() in c

int strcoll(const char *str1, const char *str2)


Parameters for strcoll():-

str1 & str2 are two Parameters which is going to use for strcoll() function in c.

Read more :- Arc function in C with graphics in c

 

Return Value :-

if return value is  < 0 then it indicates that str1 is less than str2 or Return value > 0 then it indicates that str2 is less than str1 and the last one Return value = 0 than str1 is equal to str2.

Example program for strcoll() in c

#include <stdio.h>
#include <string.h>

int main () {
   char str1[15];
   char str2[15];
   int ret;

   strcpy(str1, "abc");
   strcpy(str2, "ABC");

   ret = strcoll(str1, str2);

   if(ret > 0) {
      printf("str1 is less than str2");
   } else if(ret < 0) {
      printf("str2 is less than str1");
   } else {
      printf("str1 is equal to str2");
   }
   
   return(0);
}
, , , ,

clock() function in c with example program

 To calculate the time in c programming we can use clock() function in c which is define in time.h header file clock  can call at the beginning and end of the code for which will help to measure time.

In this process subtract the obtain values for the clock function, and then divide by CLOCKS_PER_SEC (the number of clock ticks per second)to get time.

Read also :-  ellipse function in C programming

 

Syntax for clock function in c


 #include <time.h>
     
     clock_t start, end;
     double cpu_time_used;
     
     start = clock();
     ... /* Do the work. */
     end = clock();
     cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;

Following C program will tell how to measure time with the help of clock() function in c and we also use fun() function which will help to terminate the process after enter key press to terminate.

Example program for clock() function in c



/* Program to demonstrate time taken by function fun() */
#include <stdio.h>
#include <time.h>
  
// A function that terminates when enter key is pressed
void fun()
{
    printf("fun() starts \n");
    printf("Press enter to stop fun \n");
    while(1)
    {
        if (getchar())
            break;
    }
    printf("fun() ends \n");
}
  
// The main program calls fun() and measures time taken by fun()
int main()
{
    // Calculate the time taken by fun()
    clock_t t;
    t = clock();
    fun();
    t = clock() - t;
    double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds
  
    printf("fun() took %f seconds to execute \n", time_taken);
    return 0;
}

OUTPUT :- 

clock function in c
clock function in c