41) Write a program to change case of letters in a string in C++.
In this we are going to see  a program to change case in C++ string to lowercase and uppercase  in C++ Programming Language.



The Code given below can be used in TURBO C++ Compilers: -

#include <iostream.h>
#include <conio.h>
void main()
{
    clrscr();
	char str[100];
    int x, i; 
    cout<<"Enter a string: ";
    cin>>str;
    for (i=0; str[i]!='\0';i++)
    x=i;
	for (i=0; i<x; i++) 
	{
	if(str[i]>='a' && str[i]<='z')
		str[i]=str[i]-32;
	else 
		str[i]=str[i]+32;
	}
	cout<<str;
	getch();
}


The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
using namespace std;

int main()
{
	string str;
    int x, i; 
    cout<<"Enter a string: ";
    cin>>str;
	x=str.length();
	for (i=0; i<x; i++) 
	{
	if(str[i]>='a' && str[i]<='z')
		str[i]=str[i]-32;
	else 
		str[i]=str[i]+32;
	}
	cout<<str;
	return 0;
}


#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post