Question

1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function from string.h library. You can find out more about these functions by reading through the instructions in the hw04q1.c file. Example output is given below.

#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 '\0' 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 \n 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\n\n");

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] = '\0'; // convert trailing '\n' char to '\0' (null terminator)

strcpy(strings[i], input); // copy input to 2D strings array

}

printf("\nEnter a key value for encryption: "); // prompt for integer key

scanf("%d", &key);

encryptStrings(strings, key);

printf("\nEncrypted Strings:\n");

printStrings(strings);

decryptStrings(strings, key);

printf("\nDecrypted Strings:\n");

printStrings(strings);

getchar(); // flush out newline '\n' char

printf("\nEnter a string to reverse: "); // prompt for string

fgets(input, sizeof(input), stdin); // store input string

printReversedString(input);

getchar(); // flush out newline '\n' char

printf("\nA password should be atleast 5 char long and should contain atleast one lower case char, atleast one uppercase char,");

printf("\natleast one number and only numbers and letters");

printf("\nEnter a password to validate: "); // prompt for string

fgets(input, sizeof(input), stdin); // store input string

if(isValidPassword(input))

printf("\nPassword is valid");

else

printf("\nPassword is NOT valid ");

getchar(); // keep console open

return 0;

}

output should be

:\Users \kunal source\repos \Project6\Debug>Project6.exe SE240 HW4: Pointers Enter a string: CSE 240 Enter a string: Intro to nter a string: programming Enter a string: languages nter a key value for encryption: 6 Encrypted Strings IYK&8:6 tzxu&zu xumxgssotm gtmígmky Decrypted Strings CSE 240 Intro to programming Languages nter a string to reverse: sun devils Reversed string = slived nus A password should be atleast 5 char long and should contain atleast one lower case char, atleast one uppercase char, tleast one number and only numbers and letters nter a password to validate: Asu123 Password is valid

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

#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 '\0' 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) == '\0')

{

printf("\n");

continue;

}

else*/

printf("%s\n",(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) == '\0')

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) == '\0')

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 \n 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\n\n");

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] = '\0'; // convert trailing '\n' char to '\0' (null terminator)

strcpy(strings[i], input); // copy input to 2D strings array

}

printf("\nEnter a key value for encryption: "); // prompt for integer key

scanf("%d", &key);

encryptStrings(strings, key);

printf("\nEncrypted Strings:\n");

printStrings(strings);

decryptStrings(strings, key);

printf("\nDecrypted Strings:\n");

printStrings(strings);

getchar(); // flush out newline '\n' char

printf("\nEnter a string to reverse: "); // prompt for string

fgets(input, sizeof(input), stdin); // store input string

printReversedString(input);

getchar(); // flush out newline '\n' char

printf("\nA password should be atleast 5 char long and should contain atleast one lower case char, atleast one uppercase char,");

printf("\natleast one number and only numbers and letters");

printf("\nEnter a password to validate: "); // prompt for string

fgets(input, sizeof(input), stdin); // store input string

if (isValidPassword(input))

printf("\nPassword is valid");

else

printf("\nPassword 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

*/

Add a comment
Know the answer?
Add Answer to:
1. You are given a C file which contains a partially completed program. Follow the instructions...
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
  • Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include...

    Please use Visual Studio! Let me know if you need anything else <3 #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. 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. //...

  • How do you do the commented part of this question (its the part that is bolded)?...

    How do you do the commented part of this question (its the part that is bolded)? #include <stdio.h> #include <string.h> main(){ int *p, in=10; p = &in; printf("%d\n", *p ); char arr[]="hello"; char *ptr = arr; // different ways to pass the array, at "pointer" level. printf("%s %s %s\n", arr, &arr[0], ptr); //different ways to access arr[0] printf("%c %c %c %c\n", arr[0], *ptr, *arr, ptr[0]); //different ways to access arr[1] printf("%c %c %c %c\n", arr[1], *(ptr+1), *(arr+1), ptr[1] ); //different...

  • // READ BEFORE YOU START: // You are given a partially completed program that creates a...

    // READ BEFORE YOU START: // You are given a partially completed program that creates a list of students for a school. // Each student has the corresponding information: name, gender, class, standard, and roll_number. // To begin, you should trace through the given code and understand how it works. // Please read the instructions above each required function and follow the directions carefully. // If you modify any of the given code, the return types, or the parameters, you...

  • Please answer problem #5 thank you str.c #include "str.h" #include <stdio.h> int str_len(...

    Please answer problem #5 thank you str.c #include "str.h" #include <stdio.h> int str_len(char *s) {    /* this is here so code compiles */ return 0; } /* array version */ /* concantenate t to the end of s; s must be big enough */ void str_cat(char s[], char t[]) {    int i, j;    i = j = 0;    while (s[i] != '\0')    /* find end of s */        i++;    while ((s[i++] = t[j++]) != '\0') /* copy t */        ;...

  • Can you help me make these two methods listed below work? I have code written but...

    Can you help me make these two methods listed below work? I have code written but it is not working. I would appreciate any advice or help. Function "isAPalidrome" accepts an array of characters and returns an integer. You must use pointer operations only, no arrays. This function should return 1 (true) if the parameter 'string' is a palindrome, or 0 (false) if 'string' is not a palindrome. A palindrome is a sequence of characters which when reversed, is the...

  • Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...

    Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU START: // This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user // with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for // an actor/actress, delete all movies, display all movies,...

  • In C Programming Language In this lab you will implement 4 string functions, two using array...

    In C Programming Language In this lab you will implement 4 string functions, two using array notation and two using pointers. The functions must have the signatures given below. You may not use any C library string functions. The functions are 1. int my strlen (char s ) - This function returns the number of characters in a string. You should use array notation for this function. 2. int my strcpy (char s [], char t I)- This function overwrites...

  • Please help me with those 2 question problem, please help, thanks!! Code (Please use visual studio):...

    Please help me with those 2 question problem, please help, thanks!! Code (Please use visual studio): #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...

  • Malloc function For the prelab assignment and the lab next week use malloc function to allocate...

    Malloc function For the prelab assignment and the lab next week use malloc function to allocate space (to store the string) instead of creating fixed size character array. malloc function allows user to allocate memory (instead of compiler doing it by default) and this gives more control to the user and efficient allocation of the memory space. Example int *ptr ptr=malloc(sizeof(int)*10); In the example above integer pointer ptr is allocated a space of 10 blocks this is same as creating...

  • In C programming Write the implementation for the three functions described below. The functions are called...

    In C programming Write the implementation for the three functions described below. The functions are called from the provided main function. You may need to define additional “helper” functions to solve the problem efficiently. Write a print_string function that prints the characters in a string to screen on- by-one. Write a is_identical function that compares if two strings are identical. The functions is required to be case insensitive. Return 0 if the two strings are not identical and 1 if...

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