Home »
C programs »
C Strings User-defined Functions Programs
C program to convert string into lowercase and uppercase without using library function
In this program, we will learn how to implement our own strlwr() and strupr() function in C language?
Implementing strlwr() and strupr() functions in C
This program will read a string from the user (string can be read using spaces too), and convert the string into lowercase and uppercase without using the library function.
Here we implemented two functions
- stringLwr() - it will convert string into lowercase
- stringUpr() - it will convert string into uppercase
Program to convert string into lowercase and uppercase without using library function in C
#include <stdio.h>
/********************************************************
* function name :stringLwr, stringUpr
* Parameter :character pointer s
* Description
stringLwr - converts string into lower case
stringUpr - converts string into upper case
********************************************************/
void stringLwr(char *s);
void stringUpr(char *s);
int main()
{
char str[100];
printf("Enter any string : ");
scanf("%[^\n]s",str);//read string with spaces
stringLwr(str);
printf("String after stringLwr : %s\n",str);
stringUpr(str);
printf("String after stringUpr : %s\n",str);
return 0;
}
/******** function definition *******/
void stringLwr(char *s)
{
int i=0;
while(s[i]!='\0')
{
if(s[i]>='A' && s[i]<='Z'){
s[i]=s[i]+32;
}
++i;
}
}
void stringUpr(char *s)
{
int i=0;
while(s[i]!='\0')
{
if(s[i]>='a' && s[i]<='z'){
s[i]=s[i]-32;
}
++i;
}
}
Output
Enter any string : Hello friends, How are you?
String after stringLwr : hello friends, how are you?
String after stringUpr : HELLO FRIENDS, HOW ARE YOU?
C Strings User-defined Functions Programs »