Problem statement:- Program to Convert alternate characters to capital letters.
Data requirement:-
Input Data:- str
Output Data:-str
Output Data:-str
Additional Data:- i, j
Program in C
Here is the source code of the C Program to Convert alternate characters to capital letters.
Code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[30];
printf("Enter your String:");
scanf("%[^\n]",str);
int i=0;
int j=0;
int len=sizeof(str)/sizeof(str[0]);
for(i=0;i<len;i++)
{
if(j%2==1){
if(str[i]>64 && str[i]<91 )
str[i]+=32;
}
else{
if(str[i]>96 && str[i]<123 )
str[i]-=32;
}
if(str[i]==' ')
{
continue;
}
j++;
}
printf("After Converting String is :%s",str);
}
#include<string.h>
int main()
{
char str[30];
printf("Enter your String:");
scanf("%[^\n]",str);
int i=0;
int j=0;
int len=sizeof(str)/sizeof(str[0]);
for(i=0;i<len;i++)
{
if(j%2==1){
if(str[i]>64 && str[i]<91 )
str[i]+=32;
}
else{
if(str[i]>96 && str[i]<123 )
str[i]-=32;
}
if(str[i]==' ')
{
continue;
}
j++;
}
printf("After Converting String is :%s",str);
}
Enter your String:cs info
After Converting String is :Cs InFo
After Converting String is :Cs InFo
Program in C++
Here is the source code of the C++ Program to Convert alternate characters to capital letters.
Code:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string str;
cout<<"Enter your String:";
getline(cin,str);
int i=0;
int j=0;
int len=0;
while(str[len]!='\0')
{
len++;
}
for(i=0;i<len;i++)
{
if(j%2==1){
if(str[i]>64 && str[i]<91 )
str[i]+=32;
}
else{
if(str[i]>96 && str[i]<123 )
str[i]-=32;
}
if(str[i]==' ')
{
continue;
}
j++;
}
cout<<"After Converting String is : "<<str;
}
#include<cstring>
using namespace std;
int main()
{
string str;
cout<<"Enter your String:";
getline(cin,str);
int i=0;
int j=0;
int len=0;
while(str[len]!='\0')
{
len++;
}
for(i=0;i<len;i++)
{
if(j%2==1){
if(str[i]>64 && str[i]<91 )
str[i]+=32;
}
else{
if(str[i]>96 && str[i]<123 )
str[i]-=32;
}
if(str[i]==' ')
{
continue;
}
j++;
}
cout<<"After Converting String is : "<<str;
}
Input/Output:
Enter your String:cSinfo com
After Converting String is : CsInFo CoM
After Converting String is : CsInFo CoM
Program in Java
Here is the source code of the Java Program to Convert alternate characters to capital letters.
Code:
import java.util.Scanner;
public class p8 {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str1;
System.out.println("Enter your String:");
str1=cs.nextLine();
char[] str=str1.toCharArray();
int j=0;
for(int i=0;i<str.length;i++)
{
if(j%2==1){
if(str[i]>64 && str[i]<91 )
str[i]=(char)((int)str[i]+32);
}
else{
if(str[i]>96 && str[i]<123 )
str[i]=(char)((int)str[i]-32);
}
if(str[i]==' ')
{
continue;
}
j++;
}
System.out.print("After Converting String is : ");
for(int i=0;i<str.length;i++)
System.out.print(str[i]);
cs.close();
}
}
public class p8 {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str1;
System.out.println("Enter your String:");
str1=cs.nextLine();
char[] str=str1.toCharArray();
int j=0;
for(int i=0;i<str.length;i++)
{
if(j%2==1){
if(str[i]>64 && str[i]<91 )
str[i]=(char)((int)str[i]+32);
}
else{
if(str[i]>96 && str[i]<123 )
str[i]=(char)((int)str[i]-32);
}
if(str[i]==' ')
{
continue;
}
j++;
}
System.out.print("After Converting String is : ");
for(int i=0;i<str.length;i++)
System.out.print(str[i]);
cs.close();
}
}
Input/Output:
Enter your String:
program java c python
After Converting String is : PrOgRaM jAvA c PyThOn
program java c python
After Converting String is : PrOgRaM jAvA c PyThOn
Program in Python
Here is the source code of the Python Program to Convert alternate characters to capital letters.
Code:
str=input("Enter the String:")
j=0
newStr=""
for i in range(len(str)):
if j%2==1:
if str[i]>='A' and str[i]<='Z' :
ch=chr(ord(str[i])+32)
newStr=newStr+ch
else:
newStr=newStr+str[i]
else:
if str[i] >= 'a' and str[i] <= 'z':
ch=chr(ord(str[i])-32)
newStr=newStr+ch
else:
newStr=newStr+str[i]
if str[i]==' ':
continue
j=j+1
print("After converting Your String is :", newStr)
j=0
newStr=""
for i in range(len(str)):
if j%2==1:
if str[i]>='A' and str[i]<='Z' :
ch=chr(ord(str[i])+32)
newStr=newStr+ch
else:
newStr=newStr+str[i]
else:
if str[i] >= 'a' and str[i] <= 'z':
ch=chr(ord(str[i])-32)
newStr=newStr+ch
else:
newStr=newStr+str[i]
if str[i]==' ':
continue
j=j+1
print("After converting Your String is :", newStr)
Input/Output:
Enter the String:python java c plus plus
After converting Your String is : PyThOn JaVa C pLuS pLuS
After converting Your String is : PyThOn JaVa C pLuS pLuS
0 Comments
Please do not Enter any spam link in the comment box