Question

Let us define a word to be a maximal nonempty sequence of alphanumeric characters and underscores...

Let us define a word to be a maximal nonempty sequence of alphanumeric characters and underscores (i.e. matching BRE [[:alnum:]_]\{1,\}). Consider the word to name a variable (function, type, etc.) in a programming language. We say that the word is in camel case if the subwords are introduced with an upper case letter (such as MyVerySpecialVariable). The word can start with an upper case character (UpperCamelCase) or a lower case character (lowerCamelCase). For simplicity, we assume underscores are not present in camel case. Another common naming style is snake case which are in lower case only and subwords are separated with underscores (such as my_very_special_variable, snake_case). Your task is to write two scripts which translate one style to another (camel2snake and snake2camel). In particular the scripts work as filters, i.e. they read the standard input and write to the standard output.

1. Script camel2snake translate all words which are in UpperCamelCase or lowerCamelCase are translated to snake case (words containing underscores are left untouched).

2. On the other hand script snake2camel translate every word in snake case into camel case. Default is UpperCamelCase. If option -l is present, then it translates into lowerCamelCase. Words not in snake case are left untouched (those containing capital characters).

Please help, thank you!

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

*************************************** Script for Camel To Snake Case *******************************************

script name : Camel2Snake.cpp

====================================== start=======================================

#include<iostream>
#include<string>
#include<cctype>
using namespace std;
string camelToSnake(string input)
{
string r;
char a;
for(int i=0;i<input.length();i++)
{
a=input[i];
if(isupper(a)) if i get a upper case letter then i convert it tp lower case and push '_' before the character in result string
{
a=tolower(a);
if(i!=0) r.push_back('_'); // if first letter is in Upper case then _should not appear at the start of result string
}   
r.push_back(a);
}
return r;
}
int main(int count, char *s[])
{
if(count==1)
{
cout<<"Please enter a valid input "<<endl;
return 0;
}
string k(s[1]);
string l;
l=camelToSnake(k);
cout<<l<<endl;
}

=================================== End =========================================

*************************************** Script for Snake To Camel Case *******************************************

script name : snake2Camel.cpp

:===================================== start=======================================

#include<iostream>
#include<string>
#include<cctype> //for method 'islower','isupper etc'
using namespace std;
string snakeToCamel(string input, bool toLowerCamelCase)
{
string r;
char a;
for(int i=0;i<input.length();i++)
{
a=input[i];
if(toLowerCamelCase==false && i==0) a=toupper(a);  
if(toLowerCamelCase==true && i==0) a=tolower(a);
if(a=='_') // if i found an underscore
{
i++; //increment count i
a=input[i]; //get char at i (after underscore)
if(islower(a)) a=toupper(a); // if that character is in lower case convert it to upper case
}
r.push_back(a); //pushing character into the result string
}
return r;
}


int main(int count, char *s[]) // taking inputs from command line
{

// count stores the number of command line arguments passed and s stores the arguments in char array including script

//name

if(count==1) // if no input string is passed then it will display this message, You can change this message according to you
{
cout<<"Please enter a valid input "<<endl;
return 0;
}
string l;
bool toLowerCamelCase=false;
string g(s[1]);
if(count==3) toLowerCamelCase=true; // if option given then bool variable value is set to true

l=snakeToCamel(g,toLowerCamelCase);
cout<<l<<endl;
return 0;
}

=================================== End =========================================

Add a comment
Know the answer?
Add Answer to:
Let us define a word to be a maximal nonempty sequence of alphanumeric characters and underscores...
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
  • 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...

  • 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...

  • In python Count the frequency of each word in a text file. Let the user choose...

    In python Count the frequency of each word in a text file. Let the user choose a filename to read. 1. The program will count the frequency with which each word appears in the text. 2. Words which are the spelled the same but differ by case will be combined. 3. Punctuation should be removed 4. If the file does not exist, use a ‘try-execption’ block to handle the error 5. Output will list the words alphabetically, with the word...

  • Recursion and Trees Application – Building a Word Index Make sure you have read and understood...

    Recursion and Trees Application – Building a Word Index Make sure you have read and understood ·         lesson modules week 10 and 11 ·         chapters 9 and 10 of our text ·         module - Lab Homework Requirements before submitting this assignment. Hand in only one program, please. Background: In many applications, the composition of a collection of data items changes over time. Not only are new data items added and existing ones removed, but data items may be duplicated. A list data structure...

  • This is for C++ Write a program that reads in a sequence of characters entered by...

    This is for C++ Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along with the number of times it occured. All non-alphabetic characters must...

  • Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that...

    Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that reads an input text file and counts the occurrence of individual words in the file. You will see a binary tree to keep track of words and their counts. Project description: The program should open and read an input file (named input.txt) in turn, and build a binary search tree of the words and their counts. The words will be stored in alphabetical order...

  • 1 Objective Build a hashing algorithm that is suitable for use in a Bloom Filter. Please...

    1 Objective Build a hashing algorithm that is suitable for use in a Bloom Filter. Please note that while a cryptographic hash is quite common in many Bloom Filters, the hashing algorithm to be implemented is a mix of the the following algorithmic models, specifically, a multiply & rotate hash colloquially known as a murmur hash, and an AND, rolale, & XOR hash colloquially known as an ARX hash. 2 Requirements • Inputs. Read the input file which contains strings...

  • CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the fil...

    CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the file is completely read, write the words and the number of occurrences to a text file. The output should be the words in ALPHABETICAL order along with the number of times they occur and the number of syllables. Then write the following statistics to...

  • Detecting Substrings (C++ Version) Introduction A very common task that is often performed by programs that...

    Detecting Substrings (C++ Version) Introduction A very common task that is often performed by programs that work with text files is the problem of locating a specific substring within the file. I am sure we’ve all done this many times when working with Word, Notepad, or other editors. Since we don’t have a GUI or other means of displaying the contents of a file all at once, let’s modify the problem slightly. Rather than locating a specific substring within a...

  • C++ Code

    "For two thousand years, codemakers have fought to preserve secrets while codebreakers have tried their best to reveal them." - taken from Code Book, The Evolution of Secrecy from Mary, Queen of Scots to Quantum Cryptography by Simon Singh.The idea for this machine problem came from this book.You will encrypt and decrypt some messages using a simplified version of a code in the book. The convention in cryptography is to write the plain text in lower case letters and the encrypted text in upper case...

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