Question

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.

// 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 string 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 50

// 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 reverseStrings(char strings[NUM_STRINGS][STRING_LENGTH]);

char* reverseOneString(char s[STRING_LENGTH]);

int isPalindrome(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 50 column 2D array full

of null terminators.

// The null terminator '\0' is used to denote the end of a string.

void initializeStrings(char strings[NUM_STRINGS][STRING_LENGTH])

{

char *ptr = &strings[0][0];

// enter code here

}

// Problem 2: printStrings (5 points)

// Use pointer p to traverse the 2D character array "strings" and print each

string.

// 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];

// enter code here

}

// Problem 3: reverseOneString (15 points)

// Reverse the string s by using pointer.

// Use pointer p and 'temp' char to swap 1st char with last, then 2nd char with

(last-1) and so on..

// Finally return pointer p which points to start of the reversed string.

// You may declare and use more pointers if needed.

// Hint: You might want to check if your logic works with even as well as odd

length string.

//       You can write test code to print out the reversed string to check if your

function works. (Don't include it in final submission)

char* reverseOneString(char s[STRING_LENGTH])

{

char temp;                         // not necessary to use this variable

char *p = &s[0];              // pointer to start of string

// enter code here

return p;

}

// Problem 4: reverseStrings (5 points)

// Reverse all the strings in 'strings[][]'

// For each string in 'strings', use the reverseOneString() to reverse it.

// You may declare and use more pointers if needed.

void reverseStrings(char strings[NUM_STRINGS][STRING_LENGTH])

{

char *ptr = &strings[0][0];

// enter code here

}

// Problem 5: encryptStrings (5 points)

// Use pointer ptr to traverse the 2D character array 'strings' and encrypt each

string in 2 step as follows-

// 1) Reverse the strings. Hint: Use 'reverseStrings()' for this step.

// 2) Shift the characters forward by the integer value of 'key'.

// If the string is "hello" and key = 2, reversing will get you "olleh" and adding

key to it will result in "qnnfj".

// 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 terminator to

find the end string.

//          If you could not implement reverseStrings(), skip using it in this

function. You will receive partial credit.

void encryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int key)

{

char *p = &strings[0][0];

// enter code here

}

// Problem 6: decryptStrings (5 points)

// HINT: This should be very similiar to the encryption function defined above in

encryptStrings().

// Use pointer p to traverse the 2D character array 'strings' and decrypt each

string in 2 step as follows-

// 1) Shift the characters backward by the integer value of 'key'.

// 2) Reverse the strings. Hint: Use 'reverseStrings()' for this step.

// NOTE: DO NOT decrypt the null characters.

//          If you could not implement reverseStrings(), skip using it in this

function. You will receive partial credit.

void decryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int key)

{

char *ptr = &strings[0][0];

// enter code here

}

// Problem 7: isPalindrome (10 points)

// Return 1 if string s is palindrome.

// Parse through the string to check if 1st char==last char, 2nd char == (last-1)

char, and so on..

// Return 1 if string is palindrome. Return 0 if string is not palindrome.

// A palindrome is a sequence of characters which when reversed, is the same

sequence of characters.

// Palindrome string examples: rotor, noon, madam

// Note: you may use reverseOneString() here but it is not necessary to use it.

int isPalindrome(char s[STRING_LENGTH])

{

char *p = s;

int palindrome = 1;                // edit if needed

// enter code here

return palindrome;

}

// You should study and understand how 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("\nChecking for palindrome. 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)

if (isPalindrome(input))

printf("The string is a palindrome");

else

printf("The string is not a palindrome");

getchar();

// keep VS console

open

return 0;

}

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

#define NUM_STRINGS 4
#define STRING_LENGTH 50

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 reverseStrings(char strings[NUM_STRINGS][STRING_LENGTH]);
char* reverseOneString(char s[STRING_LENGTH]);
int isPalindrome(char s[STRING_LENGTH]);


void initializeStrings(char strings[NUM_STRINGS][STRING_LENGTH]){
        char *ptr = &strings[0][0];
        int i;
        for (i = 0; i < NUM_STRINGS*STRING_LENGTH; i++) {
                *(ptr + i) = '\0';                                      //Store null terminator at each place
        }
}

void printStrings(char strings[NUM_STRINGS][STRING_LENGTH]){
        char *ptr = &strings[0][0];
        int i;
        for (i = 0; i < NUM_STRINGS*STRING_LENGTH; i++) {
                if ((i + 1) % 50 == 0)                          //To check the end of string, 
                        printf("\n");                                   //then print new line
                if (*(ptr + i) == '\0')                         //If null terminator, then dont print
                        continue;
                else                                                            //Otherwise print the character at ptr+i location
                        printf("%c", *(ptr + i));
        }
}

char* reverseOneString(char s[STRING_LENGTH]){
        char temp;                         // not necessary to use this variable
        char *p = s;              // pointer to start of string
        char* t;
        t = p;                                                  //t also point to start
        char *last = p + strlen(s)-1;   //last point to the end of string
                
                                                                        //t will move forward, last will move backward

        while (t < last) {                           //Repeat till t is earlier than last
                temp = *(t);                            //when t crosses last or t>=last stop
                *(t) = *(last);
                *(last) = temp;                         //swap letters
                t++;                                            //t now points to next character
                last--;                                         //last now points to previous character
        }       
        return p;
}

void reverseStrings(char strings[NUM_STRINGS][STRING_LENGTH]){
        char *ptr0 = &strings[0];           //Pointer to 1st string
        char *ptr1 = ptr0 + 50;                 //Pointer to 2nd string
        char *ptr2 = ptr1 + 50;                 //POinter to 3rd string
        char *ptr3 = ptr2 + 50;                 //Pointer to 4th string

        reverseOneString(ptr0);                 //reverse one by one them all
        reverseOneString(ptr1);
        reverseOneString(ptr2);
        reverseOneString(ptr3);
}

void encryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int key){
        reverseStrings(strings);                        //Reverse
        int i;
        char *p = &strings[0][0];                   //p points to beginning
        for (i = 0;i < NUM_STRINGS*STRING_LENGTH; i++) {
                if (*(p + i) == '\0' || *(p + i) == '\n') {
                        continue;
                }
                else {                                                  //Store encrypted value
                        *(p + i) = (*(p + i) + key);
                }
        }
}

void decryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int key){
        reverseStrings(strings);                        //Reverse

        char *ptr0 = &strings[0][0];
        int i;
        for (i = 0;i < NUM_STRINGS*STRING_LENGTH; i++) {
                if (*(ptr0 + i) == '\0' || *(ptr0 + i) == '\n')
                        continue;
                else
                        *(ptr0 + i) = (*(ptr0 + i) - key);      //Store decrypted value
        }
        
}

int isPalindrome(char s[STRING_LENGTH]){
        char *p = s;                                            //s points to beginning and will move forward
        char *last = p + strlen(s)-1;           //last points to end and will move backward
        int palindrome = 1;                // edit if needed
        
        while (p < last) {                                   //Check one by one until we cross middle of string
                if (*(p) != *(last)) {
                        printf("%c %c\n", *p, *last);//If mismatch, stop checking
                        palindrome = 0;
                        break;
                }
                p++;                                                    //Point to next
                last--;                                                 //Point to previous
        }
        return palindrome;
}

int main(){
        char strings[NUM_STRINGS][STRING_LENGTH];                        // will store four strings each
        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("\nChecking for palindrome. 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)
        if (isPalindrome(input))
                printf("The string is a palindrome");
        else
                printf("The string is not a palindrome");

                
        getchar();
        return 0;
}

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

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

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

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

  • 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 */        ;...

  • This program should be run on Visual Studio. Please use printf and scanf as input and output. Tha...

    This program should be run on Visual Studio. Please use printf and scanf as input and output. Thank you 6.12 Lab Exercise Ch.6b: C-string functions Create and debug this program in Visual Studio. Name your code Source.c and upload for testing by zyLabs You will write 2 functions which resemble functions in the cstring library. But they will be your own versions 1. int cstrcat(char dstDchar src) which concatenates the char array srcl to char array dstD, and returns the...

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

  • PLEASE USE MATLAB This is a basic problem that illustrates the concept of strings. A palidrome...

    PLEASE USE MATLAB This is a basic problem that illustrates the concept of strings. A palidrome is a string of characters that is read the same forwards as it is backwards. For example, racecar' is a palindrome; reversing the order of the characters leaves the string unchanged. Write a otherwise. function called isPalindrome that accepts one input and returns one output. The input str is a string of characters, and the output is 1 if str is a palindrome, For...

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

  • Hey everyone, I need help making a function with this directions with C++ Language. Can you...

    Hey everyone, I need help making a function with this directions with C++ Language. Can you guys use code like printf and fscanf without iostream or fstream because i havent study that yet. Thanks. Directions: Write a function declaration and definition for the char* function allocCat. This function should take in as a parameter a const Words pointer (Words is a defined struct) The function should allocate exactly enough memory for the concatenation of all of the strings in the...

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