Question
Help please this is C/C++ code /* * Lab12, the purpose of this lab is to...
Help please this is C/C++ code
/*
* Lab12, the purpose of this lab is to improve your skills in handling c strings
* with pointers .
* use of : strlen ,string concatenation strcat, compare strcmp,
* copy strcpy and search strrchr
*
*/
#include <cstdlib>
#include <iostream>
#include<cstring>
using namespace std;
/**
* Create a method that prompts the user for only lowercase letters to represent
* a name.
* Start a Label, then prompt the user to enter a name
* Make a static char array to represent the name with size 30
* use scanf to scan for the name , specifier for char is %s
* Declare a flag with a premise that the name is not all lowercase
* Loop the length of the name , use strlen(name) for the length of name
* The loop checks if the name is all lowercase letters
* If the user provides invalid characters other than lowercase,goto label
* @return pointer of Char
*/
char * setName(){
}
/**
* Similar to the previous method ,
* It restricts providing characters other than digits
* @return
*/
char * setBadge(){
}
/**
* Similar to the previous method ,
* It restricts providing characters other than uppercase
* @return
*/
char * setComp(){
}
int main(int argc, char** argv) {
// Declare a name pointer and set it using setName
char*name= setName();
// Declare a badge pointer and set it using the method
// Declare a company pointer and set it using the method
// Declare a character array of size 30
char email[30];
// COPY name TO email
//Concatenate email with '.'
//Concatenate badge with email
//Concatenate @ with email
//Concatenate company name with email
// Add a domain by concatenating .edu with email
// Search for the last occurrence of .
//The method returns a pointer pointing to the beginning of .edu
// Change the values next of the pointer to make '.edu' all uppercase
// Subtract -32 from the letters and that will make it uppercase
// answer will be .EDU
// Use strcmp to compare the previous pointer to ".EDU"
// Print the email using printf, the specifier is %s
//an outcome will look like : [email protected]
Answers
Given below is the code for the question. Please do rate the answer if it helped. Thank you.
/*
* Lab12, the purpose of this lab is to improve your skills in handling c strings
* with pointers .
* use of : strlen ,string concatenation strcat, compare strcmp,
* copy strcpy and search strrchr
*
*/#include <cstdlib>
#include <iostream>
#include<cstring>
using namespace std;/**
* Create a method that prompts the user for only lowercase letters to represent
* a name.
* Start a Label, then prompt the user to enter a name
* Make a static char array to represent the name with size 30
* use scanf to scan for the name , specifier for char is %s
* Declare a flag with a premise that the name is not all lowercase
* Loop the length of the name , use strlen(name) for the length of name
* The loop checks if the name is all lowercase letters
* If the user provides invalid characters other than lowercase,goto label
* @return pointer of Char
*/char * setName(){
static char name[30];
bool flag = false;
int i;
inputname:
printf("Enter a name in lowercase: ");
scanf("%s", name);
for(i = 0; i < strlen(name); i++){
if(name[i] < 'a' || name[i] > 'z')
goto inputname;
}flag = true;
return name;
}/**
* Similar to the previous method ,
* It restricts providing characters other than digits
* @return
*/char * setBadge(){
static char badge[30];
bool flag = false;
int i;
inputbadge:
printf("Enter a badge (digits): ");
scanf("%s", badge);
for(i = 0; i < strlen(badge); i++){
if(badge[i] < '0' || badge[i] > '9')
goto inputbadge;
}flag = true;
return badge;
}
/**
* Similar to the previous method ,
* It restricts providing characters other than uppercase
* @return
*/char * setComp(){
static char comp[30];
bool flag = false;
int i;
inputcomp:
printf("Enter a company in uppercase: ");
scanf("%s", comp);
for(i = 0; i < strlen(comp); i++){
if(comp[i] < 'A' || comp[i] > 'Z')
goto inputcomp;
}flag = true;
return comp;
}int main(int argc, char** argv) {
// Declare a name pointer and set it using setName
char*name= setName();
// Declare a badge pointer and set it using the method
char *badge = setBadge();
// Declare a company pointer and set it using the method
char *comp = setComp();
// Declare a character array of size 30char email[30];
// COPY name TO email
strcpy(email, name);
//Concatenate email with '.'
strcat(email, ".");
//Concatenate badge with email
strcat(email, badge);
//Concatenate @ with email
strcat(email, "@");
//Concatenate company name with email
strcat(email, comp);
// Add a domain by concatenating .edu with email
strcat(email, ".edu");
// Search for the last occurrence of .
//The method returns a pointer pointing to the beginning of .educhar *p = strrchr(email, '.');
// Change the values next of the pointer to make '.edu' all uppercase
for(char *q = p+1; *q != '\0'; q++){// Subtract -32 from the letters and that will make it uppercase
// answer will be .EDU
*q = *q - 32;
}// Use strcmp to compare the previous pointer to ".EDU"
if(strcmp(p, ".EDU") == 0)
printf("p points to .EDU\n");
else
printf("p does not point to .EDU\n");// Print the email using printf, the specifier is %s
printf("email is %s\n", email);//an outcome will look like : [email protected]
return 0;
}output
======
Enter a name in lowercase: ADDieoe
Enter a name in lowercase: sadiq
Enter a badge (digits): sfsd;k
Enter a badge (digits): 1234
Enter a company in uppercase: dfjk
Enter a company in uppercase: OKSTATE
p points to .EDU
email is [email protected]