I made a program that finds the ROT 13, but theres a little thing thats messing it up for me. Its supposed to find the ROT 13 of each character but when i run it, it gives me some funky character in return

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

int rot13(int c)
{
  if (c >= 'A' && c < 'N')
    {
      return c + 13;
    }
  if (c >= 'N' && c <= 'Z')
    {
      return c - 13;
    }
  if (c >= 'a' && c < 'n')
    {
      return c + 13;
    }
  if (c >= 'n' && c <= 'z')
    {
      return c - 13;
    }
 
}
int main(void)
{
  int c; 
  printf("Enter a character: ");
 
  while((c == getchar())!= EOF)
    { 
      char a = rot13(c);
      printf("%c", a);   
      c++;
    }
  return 0;
}

Can some one help me out please

Recommended Answers

All 4 Replies

while((c = getchar())!= EOF)

And what does your function return for non-alpha input?

while((c = getchar())!= EOF)

And what does your function return for non-alpha input?

It just returns the number
if I input 10, it returns 10
if I input ? it returns ?

This program is supposed to do only one of the characters
If i input Apple, its supposed to return N and nothing else

It just returns the number
if I input 10, it returns 10
if I input ? it returns ?

I don't see that return c; line.

I don't see that return c; line.

I put that in.

my new code looks like this

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


int rot13(int c)
{
  if (c >= 'A' && c < 'N')
    {
      return c + 13;
    }
  if (c >= 'N' && c <= 'Z')
    {
      return c - 13;
    }
  if (c >= 'a' && c < 'n')
    {
      return c + 13;
    }
  if (c >= 'n' && c <= 'z')
    {
      return c - 13;
    }
	return c;
}
int main(void)
{
  int c; 
  printf("ROT 13 Converter\n\nEnter a Character: ");
 
	while((c = getchar())!= EOF) 
	{ 
      char a = rot13(c);
      printf("%c", a);   
      c++;
    }
  return 0;
}
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.