C/C++
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   Web Development Archives FAQs C/C++

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Web Development Archives Sponsor:
  #1  
Old June 29th, 2008, 12:09 AM
Bert
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Storing input into a character array

Hi, I'm unhappy: why doesn't this work?

char enc[10000];
char temp[70];

for(int i=0;i<10000;i++){
fscanf(in,"%s",&temp);

if(temp[0]=='#')break;
else
for(int j=0;j<70;j++)
if(temp[j]!='\0')
enc[i]=temp[j];
else break;
}

The input is from a text file (.txt) that contains an encrypted
message between 1 and 10 000 letters long that may be split across
several lines in the input file; each line will contain between 1 and
70 letters inclusive and the final line will contain one #. I want the
entire message to be stored in enc (excluding the #). I thought I'm
using temp to store one line of text, checking if the first character
is a # and if not storing those characters into the next available
slots in the array enc until enc is filled up.

I'm unhappy
Bert

Reply With Quote
  #2  
Old June 29th, 2008, 06:30 AM
badc0de4
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Storing input into a character array

Bert wrote:
Hi, I'm unhappy: why doesn't this work?
>

char enc[10000];
char temp[70];
>

for(int i=0;i<10000;i++){
fscanf(in,"%s",&temp);
>

if(temp[0]=='#')break;
else
for(int j=0;j<70;j++)
if(temp[j]!='\0')
enc[i]=temp[j];

You're not updating i.
You're writing multiple times to the same location.

else break;
}

Reply With Quote
  #3  
Old June 29th, 2008, 06:30 AM
badc0de4
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Storing input into a character array

Bert wrote:
Hi, I'm unhappy: why doesn't this work?
>

char enc[10000];
char temp[70];
>

for(int i=0;i<10000;i++){
fscanf(in,"%s",&temp);
>

if(temp[0]=='#')break;
else
for(int j=0;j<70;j++)
if(temp[j]!='\0')
enc[i]=temp[j];
else break;
}
>

The input is from a text file (.txt) that contains an encrypted
message between 1 and 10 000 letters long that may be split across
several lines in the input file; each line will contain between 1 and
70 letters inclusive and the final line will contain one #. I want the
entire message to be stored in enc (excluding the #). I thought I'm
using temp to store one line of text, checking if the first character
is a # and if not storing those characters into the next available
slots in the array enc until enc is filled up.
>

I'm unhappy
Bert

Reply With Quote
  #4  
Old June 29th, 2008, 07:09 AM
Bert
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Storing input into a character array

I've changed the code and it now is:

for(int i=0;i<10000;i++){
fgets(in,"%s",temp);

if(temp[0]=='#'){break;
} else {
for(int j=0;j<70;j++)
if(temp[j]!='\0'){
enc[i]=temp[j];
}
else{
break;
}
}
}

Now it doesn't compile.
Can someone please write out what it's supposed to be? I've tried two,
three ways (this is my third attempt) to do this but I can't do it. I
find input so much harder without the string type like in C#.
If you don't write out what it's supposed to be can you write out the
code in pseudocode?
I can store an integer input easily:
fscanf(in,"%d",&rows);
but I can't get this encrypted message working.
I really want to get on to the decryption part of this question yet
I've spent DAYS trying to figure out how to store text input into a
char array and it's a really bad feeling.
My pseudocode was:

Have a counter var. for enc's elements
for(int i=0;i<10000;i++)
Get the next line of the enc. msg - store in temp[]
If the first element of the array (the first character of the
current line's input) is a #, break out of loop.
Else assign the temp[] contents in to enc[]
for(int j=0;j<70;j++)
if(temp[j]!='\0')
enc[i]=temp[j];
else break;

Reply With Quote
  #5  
Old June 29th, 2008, 07:49 AM
pete
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Storing input into a character array

Bert wrote:
Hi, I'm unhappy: why doesn't this work?
>

char enc[10000];
char temp[70];
>

for(int i=0;i<10000;i++){
fscanf(in,"%s",&temp);
>

if(temp[0]=='#')break;
else
for(int j=0;j<70;j++)
if(temp[j]!='\0')
enc[i]=temp[j];
else break;
}
>

The input is from a text file (.txt) that contains an encrypted
message between 1 and 10 000 letters long that may be split across
several lines in the input file; each line will contain between 1 and
70 letters inclusive and the final line will contain one #. I want the
entire message to be stored in enc (excluding the #). I thought I'm
using temp to store one line of text, checking if the first character
is a # and if not storing those characters into the next available
slots in the array enc until enc is filled up.
>

I'm unhappy

char enc[10000];
int i, rc;

for(i = 0; 10000 i; ++i) {
rc = getc(in);
if (rc == EF || rc == '#') {
break;
}
enc[i] = rc;
}

--
pete

Reply With Quote
  #6  
Old June 29th, 2008, 07:49 AM
pete
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Storing input into a character array

pete wrote:
Bert wrote:
>Hi, I'm unhappy: why doesn't this work?
>>

>char enc[10000];
>char temp[70];
>>

>for(int i=0;i<10000;i++){
>fscanf(in,"%s",&temp);
>>

>if(temp[0]=='#')break;
>else
>for(int j=0;j<70;j++)
>if(temp[j]!='\0')
>enc[i]=temp[j];
>else break;
>}
>>

>The input is from a text file (.txt) that contains an encrypted
>message between 1 and 10 000 letters long that may be split across
>several lines in the input file; each line will contain between 1 and
>70 letters inclusive and the final line will contain one #. I want the
>entire message to be stored in enc (excluding the #). I thought I'm
>using temp to store one line of text, checking if the first character
>is a # and if not storing those characters into the next available
>slots in the array enc until enc is filled up.
>>

>I'm unhappy
>

char enc[10000];
int i, rc;
>

for(i = 0; 10000 i; ++i) {
rc = getc(in);
if (rc == EF || rc == '#') {
break;
}
enc[i] = rc;
}

If you don't want newline characters in your array,
which I think is the case:

char enc[10000];
int rc;
int i = 0;

while (10000 i) {
rc = getc(in);
if (rc == EF || rc == '#') {
break;
}
if (rc != '\n') {
enc[i++] = rc;
}
}


--
pete

Reply With Quote
  #7  
Old June 30th, 2008, 06:49 AM
Bert
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Storing input into a character array

Why is it that from the input of

3
CPATGAHRRY
#

(in zigin.txt)

that the Y at the end of the encrypted message above isn't outputted
from the following program:

#include <stdio.h>

main()
{
//
FILE* in=fopen("zigin.txt","r");
char enc[10000];

int numrows, temp;
int numletters = 0;

fscanf(in,"%d",&numrows);

while (10000 numletters)
{
temp = getc(in);

if (temp == EF || temp == '#')
{
break;
}

if (temp != '\n') {
enc[numletters++] = temp;
}
}
//

char row1[numletters/numrows];
char lastrow[numletters/numrows];

char midrows[numrows-2][numletters/numrows+2];


int nlt_inarrays = 0;
/**/
for (int i = 0; i < (numletters/numrows); i++)
{
row1[i] = enc[nlt_inarrays];
++nlt_inarrays;
}


int remaining_letters = numletters % numrows - 3;
int nmidrowswithvalues=0;
for (int i = 0; i < remaining_letters; i++)
{
for (int j = 0; j < (numletters/numrows + 2); j++)
{
midrows[i][j] = enc[nlt_inarrays];
++nlt_inarrays;
}
++nmidrowswithvalues;
}

for (int i = 0; i < (numrows-2-nmidrowswithvalues); i++)
{
for (int j = 0; j < (numletters/numrows + 1); j++)
{
midrows[i+nmidrowswithvalues][j] = enc[nlt_inarrays];
++nlt_inarrays;
}
}


for (int i = 0; i < (numletters/numrows); i++)
{
lastrow[i] = enc[nlt_inarrays];
++nlt_inarrays;
}
/**/


printf("row1 ");
for (int i = 0; i < (numletters/numrows); i++)
{
printf("%c", row1[i]);
}
printf("\n");


for (int i = 0; i < remaining_letters; i++)
{
for (int j = 0; j < (numletters/numrows + 2); j++)
{
printf("%c", midrows[i][j]);
}
printf("\n");
}

for (int i = 0; i < (numrows-2-nmidrowswithvalues); i++)
{
for (int j = 0; j < (numletters/numrows + 1); j++)
{
printf("%c", midrows[i+nmidrowswithvalues][j]);
}
printf("\n");
}

printf("Last row ");
for (int i = 0; i < (numletters/numrows); i++)
{
printf("%c", lastrow[i]);
}
}

I'm annoyed
Bert

Reply With Quote
  #8  
Old July 4th, 2008, 05:39 PM
Bert
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Storing input into a character array

ZIGZAG CIPHER
Input File: zigin.txt File: zigout.txt Time Limit: 1 second
The latest development in cryptography, certain to be adopted by the
military, governments and anyone else who is paranoid, is the zig-zag
cipher.
To encrypt a message using the zig-zag cipher, the letters of the
message are written on the page in a zig-zag pattern. This is
illustrated below for the message "CARTGRAPHY".

C P
A T G A H
R R Y
The letters are then read one row at a time, from the top row to the
bottom. In the diagram above, the first row spells "CP", the second
spells "ATGAH" and the third spells "RRY". Finally, these are combined
to obtain the final encrypted message, which is "CPATGAHRRY".
The zig-zag cipher can be extended to use as many rows as you like.
For instance, using a zig-zag cipher with six rows, the message
"CHARLIETHEWNDERBUDGIE" is encrypted to become
"CWIHEGEAHNDRTDULEEBIR" as seen in the following diagram.
C W I
H E G E
A H N D
R T D U
L E E B
I R
Your task is to write a program that can decrypt messages written in
the zig-zag cipher. Your program must read the number of rows used in
the cipher as well as an encrypted message, and must output the
original (decrypted) message.
Input
The first line of input will consist of a single integer r, the number
of rows used in the zigzag cipher (2 <= r <= 100).
Following this will be the encrypted message. The encrypted message
will consist entirely of upper-case letters, with no spaces or
punctuation. This message may be split across several lines in the
input file; each line will contain between 1 and 70 letters inclusive.
The entire encrypted message will be between 1 and 10 000 letters
long.
Following the encrypted message will be a final line containing a
single hash (#).

Your output should consist of a single line containing the entire
decrypted message (note that this line might be very long). The output
line should contain no spaces or punctuation.
Sample Input 1 Sample 1
3 CARTGRAPHY
CPATGAHHRY
#
Sample Input 2 Sample 2
6 CHARLIETHEWNDERBUDGIE
CWIHEGE
AHNDRTDU
LEEBIR
#
Scoring
The score for each input file will be 100% if the correct decrypted
message is written to the output file and 0% otherwise.

Excluding the 'storing input into a character array' part, I've
written the code by myself to sort the encrypted message into the
first row, (total number of rows - 2) middle rows and the last row. As
far as I've tested, my program (so far) does that ALWAYS correctly.
What I've given up on is how to fetch the right element of the right
row array in the right order and regurgitate it into zigout.txt. I can
do it by pen and paper, but I can't tell the computer how to do this.
Like, it's just beyond me how people and their creativity work out the
loop to do this and how they planned their code for doing that all out
before they started writing the code in C and how they check that it's
correct and what they're thought processes are and all the rest of it.

l means letter/s
n means number/s
enc and dec end with rypted
msg means message
curr means current
m means middle

#include <stdio.h>

main()
{
FILE* in=fopen("zigin.txt","r");
char enc[10000];

int nrows, temp;
int nlmsg = 0;

fscanf(in,"%d",&nrows);

while (10000 nlmsg)
{
temp = getc(in);

if (temp == EF || temp == '#')
{
break;
}

if (temp != '\n') {
enc[nlmsg++] = temp;
}
}

int nlfrow = (nlmsg + nrows * 2 - 3) / (2 * (nrows - 1) );
int nllrow = (nlmsg + nrows - 2) / (2 * (nrows - 1) );
int nmrows = nrows - 2;
int nlm = nlmsg - nllrow - nlfrow;
int lleft = nlmsg - nllrow - 1;
int nthletter = nlfrow;
char frow[nlfrow];
char mrow[nmrows][5050];
char lrow[nllrow];

for (int i = 0; i < nlfrow; i++)
{
frow[i] = enc[i];
}

if (nlfrow == nllrow + 1)
{
for (; (nmrows - 1) >= 0; nmrows--)
{
int nlcurrow = nlm / nmrows;

if ( ( (nlcurrow + 1) * nmrows) == nlm)
{
++nlcurrow;
}

for (; (nlcurrow - 1) >= 0; nlcurrow--)
{
mrow[nmrows - 1][nlcurrow - 1] = enc[lleft];
;
;
}
}
}
else
{
for (int i = 1; i <= nmrows; i++)
{
int nlcurrow = nlm / nmrows;

if ( ( (nlcurrow + 1) * nmrows ) == nlm)
{
++nlcurrow;
}

for (int j = 1; j <= nlcurrow; j++)
{
mrow[i-1][nlcurrow-1] = enc[nthletter];
++nthletter;
;
}
}
}

nlfrow = (nlmsg + nrows * 2 - 3) / (2 * (nrows - 1) );
nlm = nlmsg - nllrow - nlfrow;
for (int i = 0; i < nllrow; i++)
{
lrow[i] = enc[nlfrow+nlm+i];
}
}


return I_GAVE_UP;

Reply With Quote
  #9  
Old July 5th, 2008, 12:00 AM
Bert
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Storing input into a character array

Ben Bacarisse wrote:
The more uniform the data structure,
the simpler the program usually is.

What's a data structure and how (have I created)/(am I using) one?

Reply With Quote
Reply

Viewing: Web Development Archives FAQs C/C++ > Storing input into a character array


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway