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     }

Dani AI

Generated

Quick summary and focused pointers for : you want a ROT13-style transform (shift letters by 13) and to convert lowercase input to uppercase. The replies from , , and all point at workable approaches (lookup table, branch ±13, and character-by-character with wrap). Those are valid — the key is handling edge cases, types, and buffer safety.

Problems to fix in the posted code

  • Two identical char temp[SIZE]; declarations and a misplaced/unfinished if block — remove duplicates and tidy braces.
  • Include the correct headers: use <string.h> for strcpy/strlen and <ctype.h> for toupper/isalpha.
  • Don’t use char i for an index; use int or size_t. char may be too small or signed.
  • fgets() leaves a trailing newline; strip it before processing. Recomputing strlen() each loop is wasteful — compute len once.

A simple, robust transformation approach

  • Read into a buffer with fgets(buffer, SIZE, stdin) and strip newline.
  • Loop with a size_t index over len = strlen(buffer). For each character c, test isalpha((unsigned char)c). If it’s a letter, pick base = isupper((unsigned char)c) ? 'A' : 'a' and rotate with the modular formula so wrap is automatic:
c = base + (c - base + 13) % 26;
  • If you must force the result to uppercase, call c = toupper((unsigned char)c).

Testing and sanity checks

  • Test inputs: A, M, N, Z, a, m, n, z, plus spaces and punctuation.
  • Compile with warnings enabled (-Wall -Wextra) and fix warnings — they’ll catch things like implicit declarations or type mismatches.
  • Build the encrypted string character-by-character (avoid unsafe strcpy into undersized buffers).

The suggestions by and about using fgets and ctype.h functions are sound; and offer alternate valid patterns. Choose the one you find clearest, but pay attention to the type, buffer, and newline issues above.

Recommended Answers

All 5 Replies

Member Avatar for Member #46692

> 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.