Hey guys i got a question to solve, can anyone help me out..

Write a C interactive program that will encode a word or sentence entered by a user. The
encryption will be done as follows:
For any word or sentence (note: blank spaces, special characters are ignored here, i.e
they are not changed) entered, each character is encrypted by changing it to the next
adjacent letter of the alphabet.
Thus if the letter ‘a’ is entered it is changed to ‘b’,
if the word ‘zorro’ is entered it will be changed to ‘apssp’,
if the sentence ‘I am a good programmer’ is entered it will be encrypted to ‘J bn b hppe
qsphsbnnfs’.

Hope someone can help me this this.. thanks !

Recommended Answers

All 16 Replies

Note that simply posting a homework assignment is against our rules. We require that you show some effort in solving the problem yourself.

Yea im writing the codes, but i am stucked about how to encode the word, i mean i stored it in an array, and from there can anyone tel me how should i encode it, that is how to change it to the next alphabet..

just help me about how to do it and i will try..

Yea im writing the codes

I see neither any code nor evidence that you've put any thought into a solution, so we've only your word to go on. Sadly, many students lie about how much work they've done and hope to trick us into doing their work for them.

I'll give you one more chance before this thread is closed as being in violation of Daniweb's rules. Please provide proof that you've done some work.

ok mate here::

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

//Declaring GLobal varibles ( for the sentence )
 char sentence[100];
 char final[100];
 int length=0;
 char ALPHA[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
 char alpha[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
 
int start()
{
   printf ("******************************************************************\n");
   printf ("*                                                                *\n");
   printf ("*            ~~~ WELCOME TO THE ECRYPTION WORLD ! ~~~            *\n");
   printf ("*                                                                *\n");
   printf ("*                                                                *\n");
   printf ("******************************************************************\n");
   input();     
}

int input()
{
   printf("\nEnter the word or the sentence: ");
   scanf("%[ ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]",&sentence);
   encryption(sentence);
}


int encryption(char sentence[100])
{   
    int i =0;
     for (length=0; length<strlen(sentence); length++)	
		{
            
			if ( sentence[i] = ALPHA [i]) 
			   {
                 final[i] = ALPHA[i+1];
                 i++;              
               }
            
          
		}
         
    printf("\nYour encrypted sentence is: %s\n",final);     
}

int main(int argc, char *argv[])
{
  
  start();
  system("PAUSE");	
  return 0;
}

Yup I think you are stucking in taking input as spaces, since you are using scanf().

nopes its taking the sentence im inserting..
scanf("%[ ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]",&sentence);
it does cater for white space and for the text..

the problem is i dont know how to change the character to the next adjacent one..

You can do it like this:--;)

#include<stdio.h>
int getline(char[],int);
main()
{
char a[100],*a1,b[100],*b1,*a2;
int c=0;
getline(a,100);
a1=a2=&a;
b1=&b;
printf("%s\n",a);  
while(*a1!=NULL)
{
     if(*a1>='A' && *a1 <='Z' || *a1>='a' && *a1<='z')
         *b1=*a1+1;
     else 
         *b1=*a1;
     a1++;
     b1++;
}
*b1=NULL;
printf("%s",b);  
  return 0;
}
int getline(char s[], int lim)
{
    int c,i;
    for(i=0;i<lim-1 && (c=getchar())!=EOF && c!='\n';++i)
       s[i]=c;
    if(c=='\n') {
         s[i]=c;
         ++i;
    }
    s[i]='\0';
    return i;
}
if ( sentence[i] = ALPHA [i]) 
           {
             final[i] = ALPHA[i]+1;
             i++;              
           }

Your error is in this part of code. logic is not correct, try another algo n I ve posted earlier how to change the character to next adjacent one..

i think i need to split the string.. i got no idea how to do that in C !!

check the code:--

int main(void)
{
    char a='a';
    a++;
    printf("%c",a);
    return 0;
}

char is an integer type of 8 bits wide.
here a stores the ascii value of a i.e, 97.
on incrementing it increases to 98 which is b and so printf is printing b as output,
thats how you have to copy each character from input string increment it and then store it to another string.

yep this part i got it.. but there is another problem,

for instance my array of characters is having "This is my favorite forum".. How will i make it that each letter will be in for a specific array location (i) ..

if i do
a=sentence;
printf("%c",a);

it return me thw whole sentence..

see the whole code that I have posted for you solution above

check the code for string:--

#include<string.h>
#include<stdio.h>
int main(void)
{
    char a[10],b[10];
    int i;
    scanf("%s",&a);
    for(i=0;i<strlen(a);i++)
    b[i]=a[i]+1;
    printf("%s",b);
    return 0;
}

thnx mate.. i have done it but a still there is a small issue:

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

//Declaring GLobal varibles ( for the sentence )
 char sentence[100];
 char final[100];
 
int start()
{
   printf ("******************************************************************\n");
   printf ("*                                                                *\n");
   printf ("*            ~~~ WELCOME TO THE ECRYPTION WORLD ! ~~~            *\n");
   printf ("*                                                                *\n");
   printf ("*                                                                *\n");
   printf ("*                                                                *\n");
   printf ("*                                                                *\n");
   printf ("******************************************************************\n");
   input();     
}

int input()
{
   printf("\nEnter the word or the sentence: ");
   scanf("%[ ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]",&sentence);
   encryption(sentence);
}


int encryption(char sentence[100])
{   
     int i;
     for (i=0; i<strlen(sentence); i++)	
		{			
           final[i] = sentence[i]+1;
        } 
        
      printf("\nYour encrypted sentence is: %s\n",final);            
    
}

int main(int argc, char *argv[])
{
  
  start();
  system("PAUSE");	
  return 0;
}

White space also are being encrypted, how can i prevent white space and special characters from being encrypted ??

whats now happining is that ascii value of space is 32 and within that loop, space and apecial characters are not checked there ASCII values are also incremented by 1 and then stored in final.
setup an if condtion for this i.e., if there is space or any special characters then no increment or you can also do this if there is only letters then increments or for any other characters no increment.

Hey avinash thanks.. u helped me alot.. I finally solve the question.. cheers mate..

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.