using arrays and adding to an array

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2006
Posts: 2
Reputation: kraze_101 is an unknown quantity at this point 
Solved Threads: 0
kraze_101 kraze_101 is offline Offline
Newbie Poster

using arrays and adding to an array

 
0
  #1
Jul 5th, 2006
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 }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: using arrays and adding to an array

 
0
  #2
Jul 5th, 2006
> 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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 89
Reputation: dilip.mathews is an unknown quantity at this point 
Solved Threads: 3
dilip.mathews's Avatar
dilip.mathews dilip.mathews is offline Offline
Junior Poster in Training

Re: using arrays and adding to an array

 
0
  #3
Jul 6th, 2006
  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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: using arrays and adding to an array

 
0
  #4
Jul 6th, 2006
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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 89
Reputation: dilip.mathews is an unknown quantity at this point 
Solved Threads: 3
dilip.mathews's Avatar
dilip.mathews dilip.mathews is offline Offline
Junior Poster in Training

Re: using arrays and adding to an array

 
0
  #5
Jul 7th, 2006
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.

Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,648
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 473
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: using arrays and adding to an array

 
0
  #6
Jul 10th, 2006
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.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC