Question

The program needs to be written in C. Write a function void camelCase(char* word) where word...

The program needs to be written in C.

Write a function void camelCase(char* word) where word consists of more than two words separated by underscore such as “random_word” or "hello_world_my_name_is_sam". camelCase() should remove underscores from the sentence and rewrite in lower camel case” (https:// en.wikipedia.org/wiki/Camel_case). Watch out for the end of the string, which is denoted by ‘\0’. You have to ensure that legal strings are given to the camelCase() function.

The program should only run when the input is lowercase letters and underscores. If the user inputs any capital letters or symbols other than '_' the program should return "The input was invalid" and exit.

NOTE: You can use the toUpperCase(...) a function provided in the skeletal code to change the case of a character from lowercase to upper case . Notice that toUpperCase() assumes that the character is currently in lower case. Therefore, you would have to check the case of a character before calling toUpperCase().

Here's the skeletal code:

#include <stdio.h>
#include <stdlib.h>

/*converts ch to upper case, assuming it is in lower case currently*/
char toUpperCase(char ch){
return ch-'a'+'A';
}


void camelCase(char* word){
/*Convert to camelCase*/
}

int main(){
/*Read the string from the keyboard */
  
/*Call camelCase*/
  
/*Print the new the string */
  
return 0;
}


Example:
If you typed in "hello_world_my_name_is_sam" the output should be "helloWorldMyNameIsSam" and exit the program.
And if you typed in "hello_WORLD_my_NAME_is_sam" the output should be "The input was invalid" and exit the program.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below:

#include <stdio.h>

#include <stdlib.h>

/*converts ch to upper case, assuming it is in lower case currently*/

char toUpperCase(char ch){

return ch-'a'+'A';

}

void camelCase(char* word){

/*Convert to camelCase*/

int i=0;

//loop through the string

while(word[i]!='\0'){

//if underscore is there

if(word[i]=='_'){

int j=i+1;

//upper case next character

word[j] = toUpperCase(word[j]);

//shift content to left by one

while(word[j]!='\0'){

word[j-1]=word[j];

j++;

}

word[j-1]='\0';

}

i++;

}

}

int main(){

/*Read the string from the keyboard */

char input[100];

printf("Enter the message : ");

scanf("%s",input);

int i=0;

int valid=1;

//check if valid string or not

while(input[i]!='\0'){

if((input[i]<'a' || input[i]>'z' ) && input[i]!='_' ){

valid = 0;

break;

}

i++;

}

//if not valid show error

if(valid==0){

printf("The input was invalid");

}else{

//else call the methods

/*Call camelCase*/

camelCase(input);

  

/*Print the new the string */

printf("%s",input);

}

  

  

return 0;

}

output:

CAUsers Mohammad Shahrukh\Desktoplc_language_workspacelCPP lowe to camelo Enter the message: hello_world_my_name_is_sam hello

CAUsers Mohammad Shahrukh\Desktoplc_language_workspacelCPPlowe to camelo Enter the message: hello_WORLD_my_NAME_is_sam The in

Add a comment
Know the answer?
Add Answer to:
The program needs to be written in C. Write a function void camelCase(char* word) where word...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 4. [8] Write a C function with prototype void letter_freq(const char wordll, int freaq ); This...

    4. [8] Write a C function with prototype void letter_freq(const char wordll, int freaq ); This function computes the number of appearances of each letter in the string word and stores them irn array freq of size 26. The letters are the 26 letters of the Latin alphabet whose ASCII values are in the range 97-122 for the lower case letters, and in the range 65-90 for the uppercase letters. You must account for uppercase and lowercase letter variants, which...

  • Write a C++ program that repeatedly reads lines until an EOF is encountered. As each line...

    Write a C++ program that repeatedly reads lines until an EOF is encountered. As each line is read, the program strips out all characters that are not upper or lower case letters or spaces, and then outputs the line. Thus, the program acts as a filter and issues no prompt. There are many ways this program could be written, but to receive full credit, you must observe the following: Place your code in a file called filterChars.cpp. The program should...

  • 10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string...

    10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string object arguments. Let's call them string1, string2, and string3. It should search string for all occurrences of string2. When it finds an occurrence of Programming Challenges string2, it should replace it with string. For example, suppose the three arguments have the following values: stringt: "the dog jumped over the fence" string 2 "the" string3: "that" With these three arguments, the function would return a...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • Write a program that reads a string from the keyboard and computes the two arrays of...

    Write a program that reads a string from the keyboard and computes the two arrays of integers upperCase, and lowerCase, each of size 26. The first one represents the frequency of upper case letters and the second one represents the frequency of lower case letters in the input string. After computing these arrays, the program prints the frequencies in the following format: The frequency of the letter A= The frequency of the letter B= Assume the input string contains only...

  • C++ PROGRAMMING Implement a bool function whose header is given below. Inside the function, declare, ask,...

    C++ PROGRAMMING Implement a bool function whose header is given below. Inside the function, declare, ask, and get a character from the user. The function will return true if the character the user enters is NOT in either of the words, otherwise it will return false. Keep in mind that uppercase and lowercase letters are not the same. You will need to change both words and the character input by the user to the same case (upper or lower) -...

  • Write in C language 5. [8] Write a function with prototype » void string_copy(const char source[],...

    Write in C language 5. [8] Write a function with prototype » void string_copy(const char source[], char destination[], int n); This function copies string source to string destination. Parameter n represents the size of array destination. If the latter array is not sufficiently large to hold the whole source string then only the prefix of the string which has room in the latter array should be copied. Note that after copying, the null character should also be included to mark...

  • I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly...

    I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly program that does the following; 1. count and display the number of words in the user input string. 2. Flip the case of each character from upper to lower or lower to upper. For example if the user types in:   "Hello thEre. How aRe yOu?" Your output should be: The number of words in the input string is: 5 The output string is : hELLO...

  • use c code to Develop a function void printToggled(char str[]) that gets a string as a...

    use c code to Develop a function void printToggled(char str[]) that gets a string as a parameter and prints every lowercase character as an uppercase one and vise versa. For example, if the string submitted as a parameter is Hello WorlD! The function should print hELLO wORLd! Hint:answer must include output

  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT