A C program to convert a given lowercase string to uppercase string without using the inbuilt string function

By | October 6, 2013

Explanation of FUNCTION:

A function is a block of code that has a name and it has a property that it is reusable i.e. it can be executed from as many different points in a C Program as required.

Function groups a number of program statements into a unit and gives it a name. This unit can be invoked from other parts of a program. A computer program cannot handle all the tasks by itself. Instead its requests other program like entities – called functions in C – to get its tasks done. A function is a self contained block of statements that perform a coherent task of same kind.

The name of the function is unique in a C Program and is Global. It means that a function can be accessed from any location within a C Program. We pass information to the function called arguments specified when the function is called. And the function either returns some value to the point it was called from or returns nothing.

We can divide a long C program into small blocks which can perform a certain task. A function is a self contained block of statements that perform a coherent task of same kind.

For writing a function and using it in the C program, we should declare the function in the MAIN function. Declaration has to done in the area before the code starts, same area where we declare data variables.

void lwr_to_upr(char*);

Function returns value and the ones which does not return any value is called and declared as void. This function which we are writing returns the gcd in decimal form as output. The values inside the round brackets are called Arguments. The Argument and return type are integer type. A function can return only one value so we have to take only one data type in which its value is expected.

Let’s identify variables needed for this program.
First variables will be the one which will save the value entered by the user S[50]. The identified variables are S[50].

Now, Selection of data type is char array data type due to the values expected are string and they will be holding value as char array data type.

Main program needs only declaration of variables and function prototype. Also messages for user and scanning string  S[50] print the message and call the Lwr_to_Upr FUNCTION which will convert the characters within the variable itself. Their is no single value to be returned hence the return value will be void.

#include<stdio.h>
void main()
{
 void lwr_to_upr(char*);
 char S[50];
 clrscr();
 printf("\nENTER STRING TO CONVERT TO UPPERCASE : ");
 scanf("%s",&S);
 lwr_to_upr(S);
 printf("\nCONVERTED UPPERCASE STRING IS :- %s",S);
 getch();
}

Let’s identify variables needed for function which is part of program outside main function.
their are no variable of the function, since character pointer is passed to the function.

In this program, we should not use inbuilt function so we are writing it in new function with new name. The base address of the string is passed to the function character pointer return true if any character is present in it or return false if any character is absent in it. check whether the character lies between a to z if yes than convert it to lowercase to uppercase. The can be down by deducting 32 to the character. see the ascii table to find out ascii number.

void lwr_to_upr(char *STR)
{
     while(*STR)
     {
      if(*STR>='a' && *STR<='z')
        *STR=*STR-32;
      STR++;
     }
}

Note:- Remember whenever you are calling a function which is returning a value it should always be assigned to a variable of same data type or called inside the printf function as variable or if void function can be called directly since return value is void (nothing).

C program code :

  1. #include<stdio.h>
  2. void main()
  3. {
  4.  void lwr_to_upr(char*);
  5.  char S[50];
  6.  clrscr();
  7.  printf("\nENTER STRING TO CONVERT TO UPPERCASE : ");
  8.  scanf("%s",&S);
  9.  lwr_to_upr(S);
  10.  printf("\nCONVERTED UPPERCASE STRING IS :- %s",S);
  11.  getch();
  12. }
  13. void lwr_to_upr(char *STR)
  14. {
  15.      while(*STR)
  16.      {
  17.       if(*STR>='a' && *STR<='z')
  18.         *STR=*STR-32;
  19.       STR++;
  20.      }
  21. }

Screen Shots :-

C_program_Lower_to_Upper

C_program_Lower_to_Upper_Output

Leave a Reply