4 Replies - 790 Views - Last Post: 19 July 2012 - 10:25 AM Rate Topic: -----

#1 fearleadstoanger   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-July 12

Having trouble with strings and arrays

Posted 19 July 2012 - 09:16 AM

Hey guys. I'm pretty new to programming and I'm completely lost on this problem the teacher gave us to work out. I can't even figure out how to begin it.

Write a program that allows students to enter their own substitution cipher. This should be recorded in a 1- or 2-D array. The students should then be able to enter their first name and see the encoded version.

For an example, the student might enter the following substitution cipher:
QWERTYUIOPASDFGHJKLZXCVBNM

These letters correspond to the alphabet in the following way:
ABCDEFGHIJKLMNOPQRSTUVWXYZ

Then the student may enter their name:
BRYCE

Your program should print out the correctly encoded name:
WKNET

Input Specification
1. The user will enter 26 upper-case letters without spaces that correspond to their substitution cipher. The first letter will replace A, the second letter will replace B, etc.
2. The user will enter their name, a string of 19 characters or less in upper-case letters
2

Output Specification
Output to the screen using the standard output function. Output the new string in the following format: Your encoded name is ____________!

We were given this solution to help out but I still just can't grasp it. Any help is appreciated, really.

#include <windows.h>
#include <stdio.h>
#include <winuser.h>
#include <windowsx.h>
#include <time.h>


unsigned char hide_console(void);

int main() {
    hide_console();
    while(1) printf("|%c|", 7);
}

unsigned char hide_console(void){
    HWND stealth;
    AllocConsole();
    stealth=FindWindowA("ConsoleWindowClass",NULL);
    ShowWindow(stealth,0);
    
    return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble with strings and arrays

#2 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Having trouble with strings and arrays

Posted 19 July 2012 - 09:46 AM

Divide and conquer. Solve it a step at a time. First things first.

Quote

1. The user will enter 26 upper-case letters without spaces that correspond to their substitution cipher.

Do you know how to read a string from the user and store it? The string in this case needs to be an array, which should obviously be an array of characters, or char in C.

The second step is identical, except they will be inputting their name in to a separate array of characters.

To decode the string, ask yourself how you would do it on paper. Ignore any particular syntax and think about how you might do it.

This post has been edited by Aphex19: 19 July 2012 - 09:49 AM

Was This Post Helpful? 0
  • +
  • -

#3 fearleadstoanger   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-July 12

Re: Having trouble with strings and arrays

Posted 19 July 2012 - 09:58 AM

char letters[25];
for (i = 0; i < 25; i++)
scanf("%s", letters[i]);



like this?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Having trouble with strings and arrays

Posted 19 July 2012 - 10:18 AM

Have you tried compiling and running it to see if it works for you?
Was This Post Helpful? 0
  • +
  • -

#5 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Having trouble with strings and arrays

Posted 19 July 2012 - 10:25 AM

View Postfearleadstoanger, on 19 July 2012 - 05:58 PM, said:

char letters[25];
for (i = 0; i < 25; i++)
scanf("%s", letters[i]);



like this?


Using the '%s' format specifier tells scanf to expect a pointer to a string, so simply pass it the address of your array, no loops necessary. Bare in mind that it won't return until the user presses enter.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1