944,028 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1806
  • C RSS
Jul 5th, 2006
0

using arrays and adding to an array

Expand Post »
i have an assignment at school where
we have to get a string entered by a user
and then the program is supposed to encrypt it
by adding 13 to A-M and -13 from M-Z so that if
i inputted A B C i would get N O P. Basically its adding 13 to
the value that the user entered and we also have to
convert all the lower case characters to upper case using
the "toupper" function.i have already tried it and i have two strings
. i copied string1 into string2 and i tried to do a for loop
to loop through all the characters and i m lost. I tried asking the teacher
but i dont understand how to do it too. i asked the TA but he wasnt much help too.
Can someone at least give me a few pointers. i am not asking for the answers right away
just a little help or pointer that i can go on or point me in the right
direction.
thank you in advance.


  1. #include <stdio.h>
  2. 17 #include<strings.h>
  3. 18
  4. 19 #define SIZE 100
  5. 20
  6. 21 void input(char string1[SIZE]); //prototype function to get user data
  7. 22 void rotate(char string1[SIZE], char string2[SIZE]); //prototype function to encrypt sentence
  8. 23 void print(char string1[SIZE], char string2[SIZE]); //prototype function to print sentence
  9. 24
  10. 25
  11. 26 int main()
  12. 27 {
  13. 28 char string1[SIZE]; //declaring the first string
  14. 29 char string2[SIZE]; //declaring the second string to be encrypted
  15. 30 input(string1); //get user sentence
  16. 31 rotate(string1, string2); //rotate user sentence
  17. 32 print(string1, string2); //print out the two strings
  18. 33
  19. 34 return 0;
  20. 35 }
  21. 36
  22. 37
  23. 38 //get a sentence to be incrypted from the user
  24. 39 void input(char string1[SIZE])
  25. 40 {
  26. 41 printf("Please enter a sentence to be encrypted \n");
  27. 42 fgets(string1,SIZE,stdin);
  28. 43 }
  29. 44
  30. 45 //rotate the sentence that the user inputted
  31. 46 void rotate(char string1[SIZE], char string2[SIZE])
  32. 47 {
  33. 48 char i;
  34. 49 char temp[SIZE];
  35. 50 char temp[SIZE];
  36. 51 strcpy(string2, string1);
  37. 52 for(i=0;i<strlen(string2);i++)
  38. 53 new[i]=temp[i]+13;
  39. 54 if(
  40. {
  41. 56 printf
  42. 57
  43. 58
  44. 59
  45. 60 }
  46. 61
  47. 62 //print out the rotated and original sentence
  48. 63 void print(char string1[SIZE], char string2[SIZE])
  49. 64 {
  50. 65 puts("original sentence");
  51. 66 printf(" %s \n", string1);
  52. 67 puts("encrypted sentence");
  53. 68 printf(" %s \n", string2);
  54. 69 }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kraze_101 is offline Offline
2 posts
since Jul 2006
Jul 5th, 2006
0

Re: using arrays and adding to an array

> i inputted A B C i would get N O P.

I think the most intuitive way would be to declare an array:-

  1. stuff[26] = {"abcdefghijklmnopqrstuvwxyz"};

Then when you read in a letter say 'a'

it finds the index that matches 'a' in stuff, the index would be 0 in this case, then you would add 13 to 0 to get 13 and you would output the index stuff[13].

If the index exceeds 26 you just wrap it back to the beginning. A simple conditional statement should suffice.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jul 6th, 2006
0

Re: using arrays and adding to an array

  1. #include <stdio.h>
  2. #include <string.h>
  3. int main()
  4. {
  5. char a[100];
  6. int len, i;
  7. printf("Enter the string");
  8. scanf("%s", a);
  9. len = strlen(a);
  10. for(i=0;i<len;i++)
  11. {
  12. if((a[i] <= 'm')&&(a[i] >= 'a'))
  13. {
  14. a[i] = a[i] + 13;
  15. }
  16. else if((a[i] > 'm')&&(a[i] <= 'z'))
  17. {
  18. a[i] = a[i] - 13;
  19. }
  20. }
  21. printf("%s", a);
  22. return 0;
  23. }

This will do the encryption part. If u enter character other than alphabets program will leave it as it is.
U can modify this as per ur requirements.
Reputation Points: 16
Solved Threads: 3
Junior Poster in Training
dilip.mathews is offline Offline
89 posts
since Jun 2006
Jul 6th, 2006
0

Re: using arrays and adding to an array

I'd prefer:
  1. #include <stdio.h>
  2. int main()
  3. {
  4. unsigned char a[100]; // unsigned to make all values positive
  5. int len, i;
  6.  
  7. printf("Enter the string");
  8. fgets(a, 100, stdin); // safer than scanf()
  9.  
  10. i = 0;
  11. while (a[i]) // use 'end' of a string as your guide
  12. {
  13. if ((a[i] >= 'a') && (a[i] <= 'z')) // check if a letter
  14. // could use isalpha() too
  15. {
  16. a[i] += 13; // encrypt
  17. if (a[i] > 'z') // check if beyond alphabet
  18. {
  19. a[i] -= 26; // convert back to beginning
  20. }
  21. }
  22. i++; // next character
  23. }
  24. printf("%s", a);
  25. return 0;
  26. }
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Jul 7th, 2006
0

Re: using arrays and adding to an array

Ur program needs slight modifications:

1) The prototype of fgets is char *fgets(char *s, int n, FILE *stream); Since the type of a[100]; is unsigned char, the program is not getting compiled for me on gcc. Can u tell me on which compiler u have compiled this prog?
2) The output I got for the input string "stuvwxyz" is "??,ƒ,.+╪"
I think u can debug this out.

Reputation Points: 16
Solved Threads: 3
Junior Poster in Training
dilip.mathews is offline Offline
89 posts
since Jun 2006
Jul 10th, 2006
0

Re: using arrays and adding to an array

I think this prog solves the prob of both the original poster and Mr. Dilip. Hope this helps. If any portability prob or any other prob in the code then let me know.
#include <stdio.h>
#include <ctype.h>
int main()
{
char a[26];
int i;
printf("Enter the string: ");
fgets(a, 100, stdin); // safer than scanf()
i = 0;
while (a[i] && isalpha(a[i])) // check input is alphabet
{
if (isupper(a[i]))
a[i] = tolower(a[i]); // is this wat the OP wanted?
// no need to check bounds due to the nature of the prob stmt
if ((a[i] >= 'a') && (a[i] <= 'm')) 
{
a[i] += 13; 
}
// to other check req as this will always be greter than 'm' and less than equal to 'z'
else 
{
a[i] -= 13;
}
a[i] = toupper(a[i]); // is this wat the OP wanted?
i++; 
} 
fputs(a, stdout);
getchar();
return 0;
}
See ya.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: pls check flowchart for errors/inefficiencies
Next Thread in C Forum Timeline: New to C and needing some guidance





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC