C++ Write a function named crazyCaps() that accepts a string as a output paramet
ID: 3872653 • Letter: C
Question
C++
Write a function named crazyCaps() that accepts a string as a output parameter and changes that string to have its capitalization altered such that the characters at even indexes are all in lowercase and odd indexes are all in uppercase. For example, if a variable s stores "Hey!! THERE!", then the call of crazyCaps(s); should change s to store "hEy!! tHeRe!". Constraint: Do not use any string functions from the standard library other than length() and at() or []. Remember that you can find the difference between upper and lowercase characters by subtracting 'A' from 'a'. Do not use the topper() or tolower() macros from cctype.
Explanation / Answer
#include <cstdlib>
#include<iostream>
#include<string.h>
using namespace std;
int main(int argc, char** argv) {
char ch[100];//declare a variable for accepting string
int i,temp;// declare variable temp for storing ascii code
cout<<"Enter the String :";//display the message to enter string
cin.getline(ch,100);//get the char array
//loop is iterated util the last char of the array
for(i=0;i <strlen(ch)-1; i++)
{
//if the char array index is odd performs if part statement
if(i%2==1)
{
//converts the char to ascii code and stores in temp
temp =int(ch[i]);
//checks whether its small letter, if yes, converts to captial letter
if((temp>96)and(temp<123))
{
ch[i]=char(temp-32);
}
}
//if the char array index is even else part is performed
else
{
//converts the char to ascii code and stores in temp
temp =int(ch[i]);
//checks whether its capital letter, if yes, converts to small letter
if((temp>64)and(temp<91))
{
ch[i]=char(temp+32);
}
}
}
//prints the string
cout<<" "<<ch;
return 0;
}
Test #1
Enter the String :Hey!! THERE!
hEy!! tHeRe!
RUN SUCCESSFUL (total time: 47s)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.