Question

In: Computer Science

(C LANGUAGE) Write a function called upperLower that inputs a line of text into char array...

(C LANGUAGE) Write a function called upperLower that inputs a line of text into char array s[100]. Output the line in uppercase letters and in lowercase letters

This is what I have so far:

void * upperLower (const char * s) {
char *word[sizeof(s)];
for(int i = 0; i < sizeof(s); i++){
*word[i] = s[i];
}
word = strtok(word, " ");

int counter = 0;
while(word != NULL){
if(counter % 2 == 0){
word[counter] = toupper(word);
printf("%s ", word);
}
else{
word[counter] = tolower(word);
printf("%s ", word);
}
counter++;
word = strtok(NULL, " ");
}
}

The method cannot be changed. Must use void * upperLower (const char * s) {

The output should be like THIS is A test

Solutions

Expert Solution

Note: The complete code is provided using the given function. The language used is C.

Code:

#include<stdio.h>
#include<string.h>

void * upperLower (const char * s){
//Length of string s
int n = strlen(s);

//New string which is the answer.
char answer[n];
int i;

//Copy the string s to answer.
for(i=0;i<n;i++)
answer[i] = s[i];

//Capital is a variable which checks whether a particular
//character should be converted to capital or small letters.
int capital = 1;
for(i=0;i<n;i++){
//If we encounter space, change the capital variable.
if(answer[i] == ' '){
if(capital == 1)
capital = 0;
else
capital = 1;
continue;
}

//If capital is 1, change each English alphabet to capital letter
//Else change each English alphabet to small letter
if(capital == 1){
if(answer[i]>='a' && answer[i]<='z'){
//ASCII code of a is 97 and A is 65. So there is a difference of 32.
answer[i]-= 32;
}
}else{
if(answer[i]>='A' && answer[i]<='Z'){
answer[i]+=32;
}
}
}
printf("The required string is:\n%s\n", answer);
}

//Main function.
int main(){
const char *s = "This is a test";
upperLower(s);
return 0;
}

Code Snippets:

Sample Output:


Related Solutions

Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds...
Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds the string 'f' in the string 'b' and replaces it with the string 't'. You can assume that f and t same length Example: char string[] = "zap";     replace(string, "ap", "oo"); --changes 'string' to "zoo".     *don't assume substring being replaced is singular, or that its own substrings are unique.     *Don't re scan letters already checked and replaced         char string[] =...
In c++ language, write a void function GetYear that prompts for and inputs the year the...
In c++ language, write a void function GetYear that prompts for and inputs the year the operator was born (type int) from standard input. The function returns the user’s birth year through the parameter list (use pass by reference) unless the user enters an invalid year, in which case a BadYear exception is thrown. To test for a bad year, think about the range of acceptable years. It must be 4 digits (i.e. 1982) and it cannot be greater than...
Write a C++ function that lets the user enter alphabet letters into a static char array...
Write a C++ function that lets the user enter alphabet letters into a static char array until either the user enters a non-alphabet letter or, it has reached the MAXSIZE. You can use the isalpha([Char]) function to check if the input is an alphabet letter or not. void fillArray (char ar[], size_t& size){ // this is the function prototype }
(In c++ language) 1A) Write a void function GetYear that prompts for and inputs the year...
(In c++ language) 1A) Write a void function GetYear that prompts for and inputs the year the operator was born (type int) from standard input. The function returns the user’s birth year through the parameter list (use pass by reference) unless the user enters an invalid year, in which case a BadYear exception is thrown. To test for a bad year, think about the range of acceptable years. It must be 4 digits (i.e. 1982) and it cannot be greater...
C Language - Programming Write a function that takes an array of ints, and the size...
C Language - Programming Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92} Write a function that takes one double parameter, and returns a char. The parameter represents...
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes...
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes the character x from string s. For s[] = “America”, a call to deleteSymbol(s, ‘a’) converts s[] = “Ame”
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that will use a while loop. The function will prompt the user to enter integers ine by one, until the user enters a negative value to stop. The function will display any integer that is less than 25. Declare and initialize any variables needed. The function takes no arguments and has a void return type.
C++ Write a function called linearSearch that takes an array as a parameter and search for...
C++ Write a function called linearSearch that takes an array as a parameter and search for a specific value inside this parameter. The function returns the frequency of a specific value in the array. In the main function: 1. Define an array called salaries of length 5. 2. Initialize the array by asking the user to input the values of its elements. 3. Define a variable called key and ask the user to enter a value for this variable. 4....
C Implement the function Append (char** s1, char** s2) that appends the second character array to...
C Implement the function Append (char** s1, char** s2) that appends the second character array to the first, replaces the first array with the result and replaces the second character array with the original first array. For example, if the first character array is "hello" and the second is "world" at the end of the function the new value of the first character array should be"helloworld" and the second array should be "hello". If the input is invalid the function...
Write a function int strlen(char s1[]) which returns the length of the char array s1.
Write a function int strlen(char s1[]) which returns the length of the char array s1.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT