#include <stdio.h> #include <string.h> #pragma warning(disable : 4996) // compil
ID: 3747844 • Letter: #
Question
#include <stdio.h>
#include <string.h>
#pragma warning(disable : 4996) // compiler directive for Visual Studio only
// Read before you start:
// You are given a partially complete program. Your job is to complete the functions in order for this program to work successfully.
// All instructions are given above the required functions, please read them and follow them carefully.
// You shoud not modify the function return types or parameters.
// You can assume that all inputs are valid. Ex: If prompted for an integer, the user will input an integer.
// You can use only the strlen() of strings.h library to check ctring length. Do not use any other string functions
// because you are supposed to use pointers for this homework.
// DO NOT use arrays to store or to index the characters in the string
// Global Macro Values. They are used to define the size of 2D array of characters
#define NUM_STRINGS 4
#define STRING_LENGTH 35
// Forward Declarations
void initializeStrings(char[NUM_STRINGS][STRING_LENGTH]);
void printStrings(char[NUM_STRINGS][STRING_LENGTH]);
void encryptStrings(char[NUM_STRINGS][STRING_LENGTH], int);
void decryptStrings(char[NUM_STRINGS][STRING_LENGTH], int);
void printReversedString(char s[STRING_LENGTH]);
int isValidPassword(char s[STRING_LENGTH]);
// Problem 1: initializeStrings (5 points)
// Use pointer p to traverse the 2D array of characters variable 'strings' (input from user in main() ) and set all characters in each
// array to a null terminator so that there is a 4 row and 35 column 2D array full of null terminators.
// The null terminator is represented by the character value '' and is used to denote the end of a string.
void initializeStrings(char strings[NUM_STRINGS][STRING_LENGTH])
{
char *ptr = &strings[0][0];
}
// Problem 2: printStrings (5 points)
// Use pointer p to traverse the 2D character array "strings" and print each of the contained strings.
// See the example outputs provided in the word document. Each string should be printed on a new line.
void printStrings(char strings[NUM_STRINGS][STRING_LENGTH])
{
char *ptr = &strings[0][0];
}
// Problem 3: encryptStrings (5 points)
// Use pointer ptr to traverse the 2D character array 'strings' and encrypt each string in 1 step as follows-
// 1) Shift the characters forward by the integer value of 'key'.
// If the string is "hello" and key = 2, we will shift those characters forward in ASCII by 2 and the result will be "jgnnq".
// Once the value of 'key' gets larger, you will extend past alphabetical characters and reach non-alphabetical characters. Thats ok.
// NOTE: DO NOT encrypt the null terminator character. Use the null terminators to find the end string.
void encryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int key)
{
char *ptr = &strings[0][0];
}
// Problem 4: decryptStrings (5 points)
// HINT: This should be very similiar to the encryption function defined above in Problem 3.
// Use pointer ptr to traverse the 2D character array 'strings' and decrypt each string in 1 step as follows-
// 1)Shift the characters backward by the integer value of 'key'.
// NOTE: DO NOT decrypt the null characters.
void decryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int key)
{
char *ptr = &strings[0][0];
}
// Problem 5: reverseStrings (15 points)
// Reverse the string s and print it, by using pointers.
// Use pointer p and 'temp' char to swap 1st char with last, then 2nd char with (last-1) and so on..
// Finally print the reversed string at the end of this function
// Hint: You might want to check if your logic works with even as well as odd length string.
void printReversedString(char s[STRING_LENGTH])
{
char temp; // not necessary to use this variable
char *p = &s[0]; // pointer to start of string
}
// Problem 6: isValidPassword (15 points)
// Return 1 if the password satisfies the requirements, else return 0.
// Password requirements: atleast 5 characters long, should contain atleast one lower case char, atleast one uppercase char,
// atleast one number and only numbers and letters.
// Valid password examples: Asu123, Cse240, abCd9. Invalid password examples: ASU123, Cidse, Asu1
// Traverse through the string by using pointer p to check if all conditions are satisfied
// Note that the password string contains char at the end when you press 'Enter' to enter the password.
int isValidPassword(char s[STRING_LENGTH])
{
char *p = &s[0];
// enter code here
return 0; // remove this line when implementing this function.
// it is added initially , so that the empty function does not give compile error.
}
// You should study and understand how this main() works.
// *** DO NOT modify it in any way ***
int main()
{
char strings[NUM_STRINGS][STRING_LENGTH]; // will store four strings each with a max length of 34
int i, key;
char input[STRING_LENGTH];
printf("CSE240 HW4: Pointers ");
initializeStrings(strings);
for (i = 0; i < NUM_STRINGS; i++)
{
printf("Enter a string: "); // prompt for string
fgets(input, sizeof(input), stdin); // store input string
input[strlen(input) - 1] = ''; // convert trailing ' ' char to '' (null terminator)
strcpy(strings[i], input); // copy input to 2D strings array
}
printf(" Enter a key value for encryption: "); // prompt for integer key
scanf("%d", &key);
encryptStrings(strings, key);
printf(" Encrypted Strings: ");
printStrings(strings);
decryptStrings(strings, key);
printf(" Decrypted Strings: ");
printStrings(strings);
getchar(); // flush out newline ' ' char
printf(" Enter a string to reverse: "); // prompt for string
fgets(input, sizeof(input), stdin); // store input string
printReversedString(input);
getchar(); // flush out newline ' ' char
printf(" A password should be atleast 5 char long and should contain atleast one lower case char, atleast one uppercase char,");
printf(" atleast one number and only numbers and letters");
printf(" Enter a password to validate: "); // prompt for string
fgets(input, sizeof(input), stdin); // store input string
if(isValidPassword(input))
printf(" Password is valid");
else
printf(" Password is NOT valid ");
getchar(); // keep console open
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <string.h>
#pragma warning(disable : 4996) // compiler directive for Visual Studio only
// Read before you start:
// You are given a partially complete program. Your job is to complete the functions in order for this program to work successfully.
// All instructions are given above the required functions, please read them and follow them carefully.
// You shoud not modify the function return types or parameters.
// You can assume that all inputs are valid. Ex: If prompted for an integer, the user will input an integer.
// You can use only the strlen() of strings.h library to check ctring length. Do not use any other string functions
// because you are supposed to use pointers for this homework.
// DO NOT use arrays to store or to index the characters in the string
// Global Macro Values. They are used to define the size of 2D array of characters
#define NUM_STRINGS 4
#define STRING_LENGTH 35
// Forward Declarations
void initializeStrings(char[NUM_STRINGS][STRING_LENGTH]);
void printStrings(char[NUM_STRINGS][STRING_LENGTH]);
void encryptStrings(char[NUM_STRINGS][STRING_LENGTH], int);
void decryptStrings(char[NUM_STRINGS][STRING_LENGTH], int);
void printReversedString(char s[STRING_LENGTH]);
int isValidPassword(char s[STRING_LENGTH]);
// Problem 1: initializeStrings (5 points)
// Use pointer p to traverse the 2D array of characters variable 'strings' (input from user in main() ) and set all characters in each
// array to a null terminator so that there is a 4 row and 35 column 2D array full of null terminators.
// The null terminator is represented by the character value '' and is used to denote the end of a string.
void initializeStrings(char strings[NUM_STRINGS][STRING_LENGTH])
{
char *ptr = &strings[0][0];
int i;
for (i = 0; i < NUM_STRINGS*NUM_STRINGS; i++)
{
*(ptr + i) = NULL;
}
}
// Problem 2: printStrings (5 points)
// Use pointer p to traverse the 2D character array "strings" and print each of the contained strings.
// See the example outputs provided in the word document. Each string should be printed on a new line.
void printStrings(char strings[NUM_STRINGS][STRING_LENGTH])
{
char *ptr = &strings[0][0];
int i,first=1;
for (i = 0; i < NUM_STRINGS*STRING_LENGTH; i++)
{
/*if (*(ptr + i) == '')
{
printf(" ");
continue;
}
else*/
printf("%s ",(ptr + i));
i += STRING_LENGTH-1;
}
}
// Problem 3: encryptStrings (5 points)
// Use pointer ptr to traverse the 2D character array 'strings' and encrypt each string in 1 step as follows-
// 1) Shift the characters forward by the integer value of 'key'.
// If the string is "hello" and key = 2, we will shift those characters forward in ASCII by 2 and the result will be "jgnnq".
// Once the value of 'key' gets larger, you will extend past alphabetical characters and reach non-alphabetical characters. Thats ok.
// NOTE: DO NOT encrypt the null terminator character. Use the null terminators to find the end string.
void encryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int key)
{
char *ptr = &strings[0][0];
int i, j;
for (i = 0; i < NUM_STRINGS*STRING_LENGTH; i++)
{
if (*(ptr + i) == '')
continue;
*(ptr + i) = *(ptr + i) + key;
}
}
// Problem 4: decryptStrings (5 points)
// HINT: This should be very similiar to the encryption function defined above in Problem 3.
// Use pointer ptr to traverse the 2D character array 'strings' and decrypt each string in 1 step as follows-
// 1)Shift the characters backward by the integer value of 'key'.
// NOTE: DO NOT decrypt the null characters.
void decryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int key)
{
char *ptr = &strings[0][0];
int i;
for (i = 0; i < NUM_STRINGS*STRING_LENGTH; i++)
{
if (*(ptr + i) == '')
continue;
*(ptr + i) = *(ptr + i) - key;
}
}
// Problem 5: reverseStrings (15 points)
// Reverse the string s and print it, by using pointers.
// Use pointer p and 'temp' char to swap 1st char with last, then 2nd char with (last-1) and so on..
// Finally print the reversed string at the end of this function
// Hint: You might want to check if your logic works with even as well as odd length string.
void printReversedString(char s[STRING_LENGTH])
{
char temp; // not necessary to use this variable
char *p = &s[0]; // pointer to start of string
int i;
for (i = strlen(s)-1; i>=0;i--)
{
printf("%c", *(p + i));
}
}
// Problem 6: isValidPassword (15 points)
// Return 1 if the password satisfies the requirements, else return 0.
// Password requirements: atleast 5 characters long, should contain atleast one lower case char, atleast one uppercase char,
// atleast one number and only numbers and letters.
// Valid password examples: Asu123, Cse240, abCd9. Invalid password examples: ASU123, Cidse, Asu1
// Traverse through the string by using pointer p to check if all conditions are satisfied
// Note that the password string contains char at the end when you press 'Enter' to enter the password.
int isValidPassword(char s[STRING_LENGTH])
{
char *p = &s[0];
int i,Capital=0,valid =1,digits=0;
// enter code here
if (strlen(s) < 5 )
{
return 0;
}
for (i = 0; i < strlen(s); i++)
{
if ((p[i] >= 65 && p[i] <= 90) || (p[i] >= 97 && p[i] <= 122))
{
if(p[i] >= 65 && p[i] <= 90)
Capital++;
continue;
}
if (p[i] >= 48 && p[i] <= 57)
{
digits++;
continue;
}
}
if (Capital < 1)
return 0;
if (digits < 1)
return 0;
else
return 1;
return 0; // remove this line when implementing this function.
// it is added initially , so that the empty function does not give compile error.
}
// You should study and understand how this main() works.
// *** DO NOT modify it in any way ***
int main()
{
char strings[NUM_STRINGS][STRING_LENGTH]; // will store four strings each with a max length of 34
int i, key;
char input[STRING_LENGTH];
printf("CSE240 HW4: Pointers ");
initializeStrings(strings);
for (i = 0; i < NUM_STRINGS; i++)
{
printf("Enter a string: "); // prompt for string
fgets(input, sizeof(input), stdin); // store input string
input[strlen(input) - 1] = ''; // convert trailing ' ' char to '' (null terminator)
strcpy(strings[i], input); // copy input to 2D strings array
}
printf(" Enter a key value for encryption: "); // prompt for integer key
scanf("%d", &key);
encryptStrings(strings, key);
printf(" Encrypted Strings: ");
printStrings(strings);
decryptStrings(strings, key);
printf(" Decrypted Strings: ");
printStrings(strings);
getchar(); // flush out newline ' ' char
printf(" Enter a string to reverse: "); // prompt for string
fgets(input, sizeof(input), stdin); // store input string
printReversedString(input);
getchar(); // flush out newline ' ' char
printf(" A password should be atleast 5 char long and should contain atleast one lower case char, atleast one uppercase char,");
printf(" atleast one number and only numbers and letters");
printf(" Enter a password to validate: "); // prompt for string
fgets(input, sizeof(input), stdin); // store input string
if (isValidPassword(input))
printf(" Password is valid");
else
printf(" Password is NOT valid ");
getchar(); // keep console open
return 0;
}
/*output
CSE240 HW4: Pointers
Enter a string: CSE 240
Enter a string: Intro to
Enter a string: programming
Enter a string: languages
Enter a key value for encryption: 6
Encrypted Strings:
IYK&8:6
Otzxu&zu
vxumxgssotm
rgtm{gmky
Decrypted Strings:
CSE 240
Intro to
programming
languages
Enter a string to reverse: sun delvis
sivled nus
A password should be atleast 5 char long and should contain atleast one lower case char, atleast one uppercase char,
atleast one number and only numbers and letters
Enter a password to validate: Asu123
Password is valid
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.