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.

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

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

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

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.

#include <stdio.h>
#include <string.h>
int main()
{
 char a[100];
 int len, i;
    printf("Enter the string");
 scanf("%s", a);
 len = strlen(a);
 for(i=0;i<len;i++)
 {
  if((a[i] <= 'm')&&(a[i] >= 'a'))
  {
   a[i] = a[i] + 13;
  }
  else if((a[i] > 'm')&&(a[i] <= 'z'))
  {
   a[i] = a[i] - 13;
  }
 }
 printf("%s", a);
 return 0;
}

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.

I'd prefer:

#include <stdio.h>
int main()
{
    unsigned char a[100];     // unsigned to make all values positive
    int len, i;

    printf("Enter the string");
    fgets(a, 100, stdin);     // safer than scanf()

    i = 0;
    while (a[i])              // use 'end' of a string as your guide
    {
        if ((a[i] >= 'a') && (a[i] <= 'z')) // check if a letter
                                            // could use isalpha() too
        {
            a[i] += 13;       // encrypt
            if (a[i] > 'z')   // check if beyond alphabet
            {
                a[i] -= 26;   // convert back to beginning
            }
        }
        i++;                  // next character
    }        
    printf("%s", a);
    return 0;
}

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.

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.

This article has been dead for over six months. Start a new discussion instead.