Please help with writing program

March 1, 2011 at 03:22:06
Specs: Windows 7
Im trying to write a program in C to decode a code the code to be converted is below

K"jcxg"c"oqwvj"."K"fq"pqv"urgcm K"jcxg"hqwt"g{gu"."dwv"ecppqv"ugg K"jcxg"c"dgf"."dwv"fq"pqv"unggr Ecp"{qw"vgnn"og"yjq"K"dgA"

when converted is should read

i have a mouth i do not speak
i have four eyes but cannot see
i have a bed but do not sleep
you tell me who i be?

It reads like this as there is an ascii shift of -2


See More: Please help with writing program

Report •

#1
March 1, 2011 at 03:24:01
Why do you want to write this program?

It seems like fairly straightforward C programming so show us what you have done so far and what problems you are having.


Report •

#2
March 1, 2011 at 03:36:58
Hi ijack,
It is part of a assignment i have to do. Problems i am having are plenty. Im using TurboC. So far i only have figured out how to convert a character into ascii but have not figured how to convert a string to ascii -2

#include <stdio.h>
main()
{
char letter = 'D';
printf ("asci code =: %d\n", letter);

}

im very new to this and ive been thrown in at the deep end and im sinking fast.


Report •

#3
March 1, 2011 at 03:53:48
but have not figured how to convert a string to ascii -2
One character at a time.

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
March 1, 2011 at 03:58:33
To expand slightly on what Razor2.3 says, a string is just an array of characters with an ASCII code 0 at the end. So just access the elements of the array one at a time. If the string is called InputString then InputString[0] is the first character, InputString[1] is the second, and so on until you have the case where InputString[n] is zero. Then you know you have reached the end of the string. (Note that the first element of an array A is A[0], not A[1] - that's just a C thing.)

Report •

#5
March 1, 2011 at 04:33:48
As i said im quite new to this and i know how to convert to ascii but not how to convert a sentence to ascii and the back to its legible form. I know i have to use pointers and string handling. Its piecing it all together where im at a loss

Report •

#6
March 1, 2011 at 04:38:43
You don't have to use pointers. Just do what I said - extract each array element of the string as a character, do what you will with it and then put it back into the array (or another array if that is more suitable). For example, in your case you need a statement like:

InputString[n] = InputString[n] - 2;


Report •

#7
March 1, 2011 at 05:10:42
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

int main()
{
int i;
int *ptr= mystring;
FILE * pFile;
char mystring [100];

pFile = fopen ("code.txt" , "r");
if (pFile == NULL) perror ("Error opening file");
else {
fgets (mystring , 100 , pFile);
puts (mystring);

for (i=0;i<100;i++;-2)
{puts

fclose (pFile);
}

return 0;
}

This is the code i have done so far. Im sorry for my lack of knowledge but i have missed a lot due to illness..


Report •

#8
March 1, 2011 at 05:25:42
I'm loath to go into much more detail as I don't want to end up doing the assignment for you. The point of the assignment is to assist your understanding.

I suggest that you study, via a book or Google, arrays and strings in C. Failing that, talk to your tutor. In the end I think you possibly need some one-to-one tuition and, without knowing anything about the course you are doing, the level you have reached, and your particular abilities, I'm not in a position to provide that. Your tutor knows all those details and is the right person to help you; that's his job.

I'm sorry if this sounds harsh, but I think it is in your own best interests in the long run.


Report •

#9
March 2, 2011 at 04:36:25
#include <stdio.h>

02 #include <string.h>

03 #include <stdlib.h>

04 #include <ctype.h>

05

06

07 main()

08 {

09

10 char* ptr; /* Declare a pointer */

11 int i;

12 int shift ;

13 int STR_LENGTH; int SIZE; /* Declare our variables */

14 char mystring [100]; /* Declare a string */

15 FILE * pFile;

16

17 pFile = fopen ("myfile.txt" , "r");

18 if (pFile == NULL) perror ("Error opening file");

19 else {

20 fgets (mystring , 100 , pFile);

21 puts (mystring);

22 fclose (pFile);

23

24 puts("Please enter the word you want coded"); /* Get the word from the user */

25 gets(mystring);

26

27 printf("Please enter the shift value...."); /* Get the shift value */

28 scanf("%d",&shift);

29

30 STR_LENGTH=strlen(mystring); /* Get the string length */

31 SIZE=sizeof(mystring); /* Get the sizeof the string */

32

33 printf("\n the value in strlen is %d size is %d",STR_LENGTH,SIZE); /* Print out the values */

34 ptr = &mystring[0]; /* Point the pointer at the first element */

35

36

37 for (i = 0; i < STR_LENGTH; i++) /* Set up the loop */

38 {

39 *ptr = mystring[i]+shift; /* Shift the first character in the string */

40 printf("\n i is:%d\n",i); /* Print the value of i */

41 ptr++; /* Increment the counter */

42 }

43

44 printf("Finished coding....the coded word is...."); /* Print out the result */

45 printf("%s",mystring);

46

47

48

49 }

50 }


this is a previous program i wrote. im trying to rearrange this one so i dont have to put in each word individually. I want the program to automatically decode for me and print to the screen.


Report •

#10
March 2, 2011 at 11:13:29
stay far away from using gets; it doesn't verify what's being read will fit inside of whatever buffer you've allocated, so it could overflow and open up a can of worms that you'd probably just want to avoid to begin with. :P fgets is much safer. fgets(mystring, sizeof mystring, stdin);

From your initial post and response #7, read your file into mystring and then loop through mystring

for(i=0; mystring[i] != '\0'; ++i) {
putchar(mystring[i] - 2);
}

I dunno what you're doing in the code above. :O


Report •

#11
March 2, 2011 at 12:05:30
Thanks shutat. I've done some more work on the code and have eventually got it to do 80 % of what i need it to do. I need to convert the code one line at a time. The code and then the decoded code underneath its relevant line.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>


main()
{

char* ptr; /* Declare a pointer */
int i;int shift = -2 ;int STR_LENGTH; int SIZE; /* Declare our variables */
char mystring [150]; /* Declare a string */

FILE * pFile;

pFile = fopen ("myfile.txt" , "r");
if (pFile == NULL) perror ("Error opening file");
else {
fgets (mystring , 150 , pFile);
puts (mystring);

STR_LENGTH=strlen(mystring); /* Get the string length */
SIZE=sizeof(mystring); /* Get the sizeof the string */



printf("\n the value in strlen is %d size is %d\n",STR_LENGTH,SIZE); /* Print out the values */
ptr = &mystring[0]; /* Point the pointer at the first element */


for (i = 0; i < STR_LENGTH; i++) /* Set up the loop */
{
*ptr = mystring[i]+shift; /* Shift the first character in the string */

ptr++; /* Increment the counter */
}
printf("The code is!!\n"); /* Print out the result */
printf("%s",mystring);


fclose (pFile);
}
}



Report •

#12
March 2, 2011 at 20:06:02
do you mean the data file? if each coded string is on a separate line, then use a loop

while(fgets(mystring, sizeof mystring, pFile) != NULL) {
   ...
}

if the data is contiguous, look for and break on a deliminator...

int len = 0;  // length of each line
ptr = mystring;
while(*ptr) {
   if(*ptr + shift == deliminator) {
      printf("\nline length %d\n", len);
      len = 0;
   }
   printf("%c", *(ptr++) + shift);
   ++len;
}
printf("\nline length %d\n", len); // pick up last line length

Maybe something like that.


Report •

#13
March 3, 2011 at 04:51:58
What i need is the text file line by line and under each line is the legible form
so my output should look like below

K"jcxg"c"oqwvj"."K"fq"pqv"urgcm
i have a mouth i do not speak
K"jcxg"hqwt"g{gu"."dwv"ecppqv"ugg
i have four eyes but cannot see
K"jcxg"c"dgf"."dwv"fq"pqv"unggr
i have a bed but do not sleep
Ecp"{qw"vgnn"og"yjq"K"dgA"
you tell me who i be?


Report •

#14
March 3, 2011 at 12:19:08
if the data file has lines of encoded text, use a loop for the file read. inside of the loop, print mystring, which will give you the coded string on each line, and then loop through mystring to decode it.

If the data file is just one big blob, what you might try is strtok and whatever deliminator you need; for example, a newline char

char tok[2]; 
tok[0] = '\n' - shift; // get shifted delim
tok[1] = '\0';

ptr = strtok(mystring, tok);
while(ptr) {
   puts(ptr); // coded string
   while(*ptr) {
      putchar(*(ptr++) + shift); // decoded string
   }
   // format as desired
   ptr = strtok(NULL, tok);
}

maybe something like that.


Report •

Ask Question