am beginner in C..am trying out some questions in books n i want to write a small encryption program that will encode a word..cn any1one help?i want to convert a character such as 'a' to the next character of the alphabet that is 'b'...how should i proceed?

Hmm.. i am not sure that i have got your question but:

int main( void )
{
    char name[BUFSIZ] = {'\0'} ;
    printf ( "Enter the string: " ) ;
    fgets( name, BUFSIZ, stdin ) ;

    int i = 0 ;
    for( i = 0; i < strlen( name ) - 1; ++i )
    {
        name[i] = name[i] + 1 ;
    }

    printf( "%s", name ) ;
}

am beginner in C..am trying out some questions in books n i want to write a small encryption program that will encode a word..cn any1one help?i want to convert a character such as 'a' to the next character of the alphabet that is 'b'...how should i proceed?

Might be a stupid response, but do you know that chars and ints are able to be used interchangably (with a few restrictions) in C?

For example:

char a = 'a' + 1;

is the equivalent to

char a = 'b';

and equivalent to

int b = 'a' + 1;
char a = b;

a is equal in all 3 examples
it normally just depends how you format the output (as int or char)

thanks S.O.S..it works but wat should i modify to make the program ignore blank spaces and special characters..so that i can encode a sentence..n wat abt the letter 'z'?i want it to be changed to 'a'..someone suggested me that i should use ASCII...how to do that?

1. To make the program modify only characters you can use the function isalpha ( my_char ) which accepts a character and returns true or non zero value if the char is a alphabetic character. Thus before encrypting the character check the char using this function and it should work fine.

2. To make the program loop back from 'a' to 'z' you have to use some ascii mathematics... ((my_char + 1) - 'a') % 26 ; This should do the trick for loopback.

Consider my_char = 'z' then
(( 'z' + 1 - a ) % 26) + 'a' => 26 % 26 + 'a' => 0 + 'a'=> 'a'

HOpe it helped, bye.

can u explain me what does did you meant by:-Consider my_char = 'z' then (( 'z' + 1 - a ) % 26) + 'a' => 26 % 26 + 'a' => 0 + 'a'=> 'a' n thks for helpin me...

Hmm.. k now follow closely.
Suppose the character under consideration is the char variable my_char. You want to encrypt "z" and my_char is currently is the character 'z'.

1. Just to point out to you that char values are actually integers ranging from 0 to 255 ( 8 bits ). And according to the standard ASCII table the integer value for 'a' is 97 and 'z' is 122. So working with characters is actually working with integer or pure numbers.

2. What you want is to encrypt the char by addign one to it -- so first we will follow that step. my_char + 1 // this will give the required char 3. THe above process will work fine from 'a' to 'y' but when 'z' is encountered we want to loop back. So the extra work is required for this.

4. Next i do (my_char + 1 - 'a') % 26 This step is called normalizing or brinign the result in the required range that is from 1 to 26 ( here a -> 1st char, z -> 26 th char )

5. Now the output which i get will always be in the range of 0 to 25 irrespective of the character considered (try it out yourself and you will see)

6. But for our purpose we want actual char values and not normalized values so we again add 'a' to the whole result so that the result obtained are character values or integers ranging from 'a' to 'z' irrespective of the character entered by the user.

Hope it helped, if any more doubts post again.

hi!well, i asked a fren about the following line of code and he said i shoudnt use BUFSIZ..can u tell another way to rewrite this code..n cud u explain me wat is the code for?char name[BUFSIZ] = {'\0'} ;

Is your friend a well known software engineer or is there any specific reason you should listen to him ?
If he is a student just like you then you would be better off gettign your thoughts clarified at the forums rather than getting all the wrong ideas from each other.

And as far as the line char name[BUFSIZ] = {'\0'} ; It creates a character array name of size BUFSIZ which is a constant defined as #define BUFSIZ 512 in the standard library.

And by following it with {'\0'} , i am initializing all the elemnts of the array to null value i.e. '\0' .

thks for making things clear...my fren 2 is a 2nd year student in sofware engineering...infact i was confused by what he said..n u cleared my doubts..thks again!cya

hi!
I've tried another way of doin this.. Can u tell me how do i modify to make the program run forever?

the code is:-

#include main(){    char line[100];int count;{     printf("Enter a line of text below:\n");      scanf("%[^\n]", line);     printf("\n");     printf("\nEncoded text:\n");     for(count=0; line[count]!='\0'; ++count)     {                  if(((line[count]>='A') && (line[count]='a') && (line[count]

Edited 3 Years Ago by pyTony: fixed formatting

This question has already been answered. Start a new discussion instead.