Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #74314 > unrolled thread

Program to cipher a text, new thread, fresh one

Started byAlla _ <modelling.data@gmail.com>
First post2015-10-30 08:23 -0700
Last post2015-11-04 09:50 +0100
Articles 20 on this page of 68 — 15 participants

Back to article view | Back to comp.lang.c


Contents

  Program to cipher a text, new thread, fresh one Alla _ <modelling.data@gmail.com> - 2015-10-30 08:23 -0700
    Re: Program to cipher a text, new thread, fresh one Bartc <bc@freeuk.com> - 2015-10-30 15:39 +0000
      Re: Program to cipher a text, new thread, fresh one Alla _ <modelling.data@gmail.com> - 2015-10-30 09:33 -0700
      Re: Program to cipher a text, new thread, fresh one Richard Heathfield <rjh@cpax.org.uk> - 2015-11-01 09:33 +0000
    Re: Program to cipher a text, new thread, fresh one Mark Storkamp <mstorkamp@yahoo.com> - 2015-10-30 11:17 -0500
      Re: Program to cipher a text, new thread, fresh one Alla _ <modelling.data@gmail.com> - 2015-10-30 09:32 -0700
        Re: Program to cipher a text, new thread, fresh one Richard Heathfield <rjh@cpax.org.uk> - 2015-11-01 09:38 +0000
    Re: Program to cipher a text, new thread, fresh one "Osmium" <r124c4u102@comcast.net> - 2015-10-30 10:55 -0600
      Re: Program to cipher a text, new thread, fresh one Richard Heathfield <rjh@cpax.org.uk> - 2015-11-01 10:07 +0000
        Re: Program to cipher a text, new thread, fresh one Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-01 10:42 +0000
          Re: Program to cipher a text, new thread, fresh one Richard Heathfield <rjh@cpax.org.uk> - 2015-11-01 11:35 +0000
            Re: Program to cipher a text, new thread, fresh one James Kuyper <jameskuyper@verizon.net> - 2015-11-01 08:33 -0500
            Re: Program to cipher a text, new thread, fresh one Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-01 16:09 +0000
              Re: Program to cipher a text, new thread, fresh one Bartc <bc@freeuk.com> - 2015-11-02 12:43 +0000
                Re: Program to cipher a text, new thread, fresh one Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-02 13:25 +0000
                Re: Program to cipher a text, new thread, fresh one Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-02 06:13 -0800
            Re: Program to cipher a text, new thread, fresh one Keith Thompson <kst-u@mib.org> - 2015-11-01 12:26 -0800
        Re: Program to cipher a text, new thread, fresh one supercat@casperkitty.com - 2015-11-01 07:59 -0800
          Re: Program to cipher a text, new thread, fresh one Richard Heathfield <rjh@cpax.org.uk> - 2015-11-02 13:48 +0000
            Re: Program to cipher a text, new thread, fresh one Bartc <bc@freeuk.com> - 2015-11-02 14:06 +0000
              Re: Program to cipher a text, new thread, fresh one Richard Heathfield <rjh@cpax.org.uk> - 2015-11-02 14:51 +0000
                Re: Program to cipher a text, new thread, fresh one Bartc <bc@freeuk.com> - 2015-11-02 15:48 +0000
                  Re: Program to cipher a text, new thread, fresh one Richard Heathfield <rjh@cpax.org.uk> - 2015-11-02 16:00 +0000
                    Re: Program to cipher a text, new thread, fresh one asetofsymbols@gmail.com - 2015-11-03 01:40 -0800
                      Re: Program to cipher a text, new thread, fresh one Richard Heathfield <rjh@cpax.org.uk> - 2015-11-03 11:16 +0000
                      Re: Program to cipher a text, new thread, fresh one "Osmium" <r124c4u102@comcast.net> - 2015-11-03 05:21 -0600
                        Re: Program to cipher a text, new thread, fresh one asetofsymbols@gmail.com - 2015-11-03 03:41 -0800
                          Re: Program to cipher a text, new thread, fresh one Richard Heathfield <rjh@cpax.org.uk> - 2015-11-03 11:53 +0000
                            Re: Program to cipher a text, new thread, fresh one "Osmium" <r124c4u102@comcast.net> - 2015-11-03 05:57 -0600
                              Re: Program to cipher a text, new thread, fresh one Richard Heathfield <rjh@cpax.org.uk> - 2015-11-03 12:48 +0000
                                Re: Program to cipher a text, new thread, fresh one Rosario19 <Ros@invalid.invalid> - 2015-11-04 09:09 +0100
                                  Re: Program to cipher a text, new thread, fresh one Rosario19 <Ros@invalid.invalid> - 2015-11-04 09:37 +0100
                                Re: Program to cipher a text, new thread, fresh one Alla _ <modelling.data@gmail.com> - 2015-11-04 07:09 -0800
                          Re: Program to cipher a text, new thread, fresh one "Osmium" <r124c4u102@comcast.net> - 2015-11-03 05:53 -0600
                          Re: Program to cipher a text, new thread, fresh one asetofsymbols@gmail.com - 2015-11-03 04:29 -0800
                            Re: Program to cipher a text, new thread, fresh one "Osmium" <r124c4u102@comcast.net> - 2015-11-03 06:42 -0600
        Re: Program to cipher a text, new thread, fresh one Geoff <geoff@invalid.invalid> - 2015-11-01 08:07 -0800
        Re: Program to cipher a text, new thread, fresh one Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-02 02:26 -0800
          Re: Program to cipher a text, new thread, fresh one Rosario19 <Ros@invalid.invalid> - 2015-11-06 07:53 +0100
            Re: Program to cipher a text, new thread, fresh one Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-07 10:01 -0800
    Re: Program to cipher a text, new thread, fresh one Alla _ <modelling.data@gmail.com> - 2015-10-30 10:50 -0700
      Re: Program to cipher a text, new thread, fresh one Alla _ <modelling.data@gmail.com> - 2015-10-30 10:53 -0700
        Re: Program to cipher a text, new thread, fresh one Barry Schwarz <schwarzb@dqel.com> - 2015-10-30 11:52 -0700
          Re: Program to cipher a text, new thread, fresh one Alla _ <modelling.data@gmail.com> - 2015-10-30 12:12 -0700
            Re: Program to cipher a text, new thread, fresh one Barry Schwarz <schwarzb@dqel.com> - 2015-10-30 21:05 -0700
              Re: Program to cipher a text, new thread, fresh one Alla _ <modelling.data@gmail.com> - 2015-10-30 23:58 -0700
                Re: Program to cipher a text, new thread, fresh one Barry Schwarz <schwarzb@dqel.com> - 2015-10-31 03:09 -0700
                  Re: Program to cipher a text, new thread, fresh one Alla _ <modelling.data@gmail.com> - 2015-10-31 03:47 -0700
                    Re: Program to cipher a text, new thread, fresh one Barry Schwarz <schwarzb@dqel.com> - 2015-10-31 11:42 -0700
        Re: Program to cipher a text, new thread, fresh one Barry Schwarz <schwarzb@dqel.com> - 2015-10-30 12:04 -0700
        Re: Program to cipher a text, new thread, fresh one Alla _ <modelling.data@gmail.com> - 2015-10-30 12:10 -0700
    Re: Program to cipher a text, new thread, fresh one "Osmium" <r124c4u102@comcast.net> - 2015-10-30 13:17 -0600
      Re: Program to cipher a text, new thread, fresh one Alla _ <modelling.data@gmail.com> - 2015-10-31 00:04 -0700
        Re: Program to cipher a text, new thread, fresh one Alla _ <modelling.data@gmail.com> - 2015-10-31 00:25 -0700
    Re: Program to cipher a text, new thread, fresh one Keith Thompson <kst-u@mib.org> - 2015-10-30 12:34 -0700
      Re: Program to cipher a text, new thread, fresh one Alla _ <modelling.data@gmail.com> - 2015-10-30 21:49 -0700
        Re: Program to cipher a text, new thread, fresh one Keith Thompson <kst-u@mib.org> - 2015-10-31 13:18 -0700
    Re: Program to cipher a text, new thread, fresh one Alla _ <modelling.data@gmail.com> - 2015-10-31 00:29 -0700
    Re: Program to cipher a text, new thread, fresh one Alla _ <modelling.data@gmail.com> - 2015-10-31 01:12 -0700
      Re: Program to cipher a text, new thread, fresh one Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-31 12:24 +0000
        Re: Program to cipher a text, new thread, fresh one Alla _ <modelling.data@gmail.com> - 2015-10-31 06:11 -0700
        Re: Program to cipher a text, new thread, fresh one Bartc <bc@freeuk.com> - 2015-10-31 15:58 +0000
          Re: Program to cipher a text, new thread, fresh one Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-31 17:37 +0000
            Re: Program to cipher a text, new thread, fresh one Bartc <bc@freeuk.com> - 2015-10-31 18:14 +0000
              Re: Program to cipher a text, new thread, fresh one Keith Thompson <kst-u@mib.org> - 2015-10-31 13:29 -0700
                Re: Program to cipher a text, new thread, fresh one Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-31 20:35 +0000
              Re: Program to cipher a text, new thread, fresh one Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-31 20:32 +0000
    Re: Program to cipher a text, new thread, fresh one Rosario19 <Ros@invalid.invalid> - 2015-11-04 09:50 +0100

Page 3 of 4 — ← Prev page 1 2 [3] 4  Next page →


#74349

FromAlla _ <modelling.data@gmail.com>
Date2015-10-30 10:50 -0700
Message-ID<245098a5-ba39-472c-91f5-b01b756005c9@googlegroups.com>
In reply to#74314
Based on suggestions made by Keith Thompson in another thread,
I have corrected certain issues in the program.

//** Program to cipher the text based on vigenere algorithm **/

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

int is_word(const char *string);
int word_index(const char *text, const char *word);
void new_text(char text[], char key_word[]);

int main (int argc, char *argv[])
{
    char message[80];
    
    if (argc == 2 && is_word(argv[1]))
    {
        char *key = argv[1];
        
        printf("Please, enter your message: \n");
        
        if (fgets (message, sizeof message, stdin) != NULL)
        {
            new_text(message, key);
        }
    }
    
    else
    {
        printf("\a");
        printf("Please, provide one argument - a word\n");
        return 1;
    }

    return 0;
}
            
int is_word(const char *string)
{
    int word = 0;
    
    if (string != NULL)
    {
        word = 1;
        
        while(word != 0 && *string != '\0')
        {
            word = (isalpha(*string++)); /** the pointer refers to the address of the first
                                          character, hence *string++ is used **/
                                         /** if the string is not an alphabetical one, then
                                          isalpha returns 0, the while loop terminates, and 
                                          the function returns 0 as the value of the word**/
        }
    }
    
    return word;
}

int word_index(const char *text, const char *word)
{
    int i, index;
    int word_len = strlen(word);
    int text_len = strlen(text);
    
    for(i = 0; i < text_len; i++)
    {
        if (islower(word[i % word_len]))
        {
            index = word[i % word_len] - 'a';
            return index;
        }
        
        else if (isupper(word[i % word_len]))
        {
            index = word[i % word_len] - 'A';
            return index;
        }
        else
            return -1;
    }
    
    return -1;
}

void new_text(char text[], char key_word[])
{
    int i;
    int len = strlen(text);
    char new_letter;
    
    for (i = 0; i < len; i++)
    {
        int letter = text[i];
            
        if (islower (letter))
        {
            new_letter = (letter - 'a' + word_index(text, key_word)) % 26 + 'a';
            putchar("%c", new_letter);
        }
            
        else if (isupper (letter))
        {
            new_letter = (letter - 'A' + word_index(text, key_word)) % 26 + 'A';
            putchar("%c", new_letter);
        }
        
        else
            putchar("%c", text[i]);
    }
    printf ("\n");
}

[toc] | [prev] | [next] | [standalone]


#74350

FromAlla _ <modelling.data@gmail.com>
Date2015-10-30 10:53 -0700
Message-ID<afd770ec-3298-492e-8e70-d9505ff76b44@googlegroups.com>
In reply to#74349
did not correct putchar function, my mistake. now it is correct.

//** Program to cipher the text based on vigenere algorithm **/

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

int is_word(const char *string);
int word_index(const char *text, const char *word);
void new_text(char text[], char key_word[]);

int main (int argc, char *argv[])
{
    char message[80];
    
    if (argc == 2 && is_word(argv[1]))
    {
        char *key = argv[1];
        
        printf("Please, enter your message: \n");
        
        if (fgets (message, sizeof message, stdin) != NULL)
        {
            new_text(message, key);
        }
    }
    
    else
    {
        printf("\a");
        printf("Please, provide one argument - a word\n");
        return 1;
    }

    return 0;
}
            
int is_word(const char *string)
{
    int word = 0;
    
    if (string != NULL)
    {
        word = 1;
        
        while(word != 0 && *string != '\0')
        {
            word = (isalpha(*string++)); /** the pointer refers to the address of the first
                                          character, hence *string++ is used **/
                                         /** if the string is not an alphabetical one, then
                                          isalpha returns 0, the while loop terminates, and 
                                          the function returns 0 as the value of the word**/
        }
    }
    
    return word;
}

int word_index(const char *text, const char *word)
{
    int i, index;
    int word_len = strlen(word);
    int text_len = strlen(text);
    
    for(i = 0; i < text_len; i++)
    {
        if (islower(word[i % word_len]))
        {
            index = word[i % word_len] - 'a';
            return index;
        }
        
        else if (isupper(word[i % word_len]))
        {
            index = word[i % word_len] - 'A';
            return index;
        }
        else
            return -1;
    }
    
    return -1;
}

void new_text(char text[], char key_word[])
{
    int i;
    int len = strlen(text);
    char new_letter;
    
    for (i = 0; i < len; i++)
    {
        int letter = text[i];
            
        if (islower (letter))
        {
            new_letter = (letter - 'a' + word_index(text, key_word)) % 26 + 'a';
            putchar(new_letter);
        }
            
        else if (isupper (letter))
        {
            new_letter = (letter - 'A' + word_index(text, key_word)) % 26 + 'A';
            putchar(new_letter);
        }
        
        else
            putchar(text[i]);
    }
    printf ("\n");
}

[toc] | [prev] | [next] | [standalone]


#74355

FromBarry Schwarz <schwarzb@dqel.com>
Date2015-10-30 11:52 -0700
Message-ID<84e73b9mf8qc60l58cebluuost577bgkv7@4ax.com>
In reply to#74350
On Fri, 30 Oct 2015 10:53:47 -0700 (PDT), Alla _
<modelling.data@gmail.com> wrote:

>int word_index(const char *text, const char *word)
>{
>    int i, index;
>    int word_len = strlen(word);
>    int text_len = strlen(text);
>    
>    for(i = 0; i < text_len; i++)

What is the purpose of this for loop?  IT NEVER ITERATES.  It either
executes 0 times or 1 time, never multiple times.

You never use any data from text.  It is illogical for the length of
the string pointed to by text determine if you examine word at all.  

Unless text happens to point to an empty string, the function returns
a value between -1 and 25 based SOLELY on the SINGLE character pointed
to by word.  Why not pass that character instead of making the
function dereference a pointer?

>    {
>        if (islower(word[i % word_len]))

i can only be 0 so the computed index is always 0.  The computation
will fail in the pathological case where word points to an empty
string.

>        {
>            index = word[i % word_len] - 'a';
>            return index;
>        }
>        
>        else if (isupper(word[i % word_len]))
>        {
>            index = word[i % word_len] - 'A';
>            return index;
>        }
>        else
>            return -1;
>    }
>    
>    return -1;

This statement may be needed to silence a compiler warning but can be
executed only when text points to an empty string.
>}

-- 
Remove del for email

[toc] | [prev] | [next] | [standalone]


#74358

FromAlla _ <modelling.data@gmail.com>
Date2015-10-30 12:12 -0700
Message-ID<08e57114-ba74-4831-9414-c860f4245d5d@googlegroups.com>
In reply to#74355
On Friday, October 30, 2015 at 10:53:10 PM UTC+4, Barry Schwarz wrote:
> On Fri, 30 Oct 2015 10:53:47 -0700 (PDT), Alla _
> <modelling.data@gmail.com> wrote:
> 
> >int word_index(const char *text, const char *word)
> >{
> >    int i, index;
> >    int word_len = strlen(word);
> >    int text_len = strlen(text);
> >    
> >    for(i = 0; i < text_len; i++)
> 
> What is the purpose of this for loop?  IT NEVER ITERATES.  It either
> executes 0 times or 1 time, never multiple times.
> 
> You never use any data from text.  It is illogical for the length of
> the string pointed to by text determine if you examine word at all.  


I accidentally removed one line that was always there
in this loop. I have corrected my mistake. Therefore, the loop
iterates until the length of the text is reached.

[toc] | [prev] | [next] | [standalone]


#74398

FromBarry Schwarz <schwarzb@dqel.com>
Date2015-10-30 21:05 -0700
Message-ID<jse83btlki6fkglml1nq3tdi3olgkfvcse@4ax.com>
In reply to#74358
On Fri, 30 Oct 2015 12:12:25 -0700 (PDT), Alla _
<modelling.data@gmail.com> wrote:

>On Friday, October 30, 2015 at 10:53:10 PM UTC+4, Barry Schwarz wrote:
>> On Fri, 30 Oct 2015 10:53:47 -0700 (PDT), Alla _
>> <modelling.data@gmail.com> wrote:
>> 
>> >int word_index(const char *text, const char *word)
>> >{
>> >    int i, index;
>> >    int word_len = strlen(word);
>> >    int text_len = strlen(text);
>> >    
>> >    for(i = 0; i < text_len; i++)
>> 
>> What is the purpose of this for loop?  IT NEVER ITERATES.  It either
>> executes 0 times or 1 time, never multiple times.
>> 
>> You never use any data from text.  It is illogical for the length of
>> the string pointed to by text determine if you examine word at all.  
>
>
>I accidentally removed one line that was always there
>in this loop. I have corrected my mistake. Therefore, the loop
>iterates until the length of the text is reached.

The update in your message stamped 2 minutes before this one
accomplished two things:

     It rendered the fist "else" statement superfluous.  If isalpha is
true, the exactly one of islower or isupper must be true so the else
is never executed.

     It did allow the for loop to iterate.  The function now returns
after processing the first alphabetic character.  The function still
returns the same value regardless of how many times you call it in the
function new_text.

     

-- 
Remove del for email

[toc] | [prev] | [next] | [standalone]


#74405

FromAlla _ <modelling.data@gmail.com>
Date2015-10-30 23:58 -0700
Message-ID<e63346ff-acef-4bb5-ad9a-950a7eedbf14@googlegroups.com>
In reply to#74398
> 
>      It did allow the for loop to iterate.  The function now returns
> after processing the first alphabetic character.  The function still
> returns the same value regardless of how many times you call it in the
> function new_text.
> 

I see what you mean, but I am not sure I understand the reason
correctly. Please, take a look at my explanation below (the very
last paragraph).

I assume you are talking about the following version of the 
word_index function:


int word_index(const char *text, const char *word)
{
    int i, index;
    int word_len = strlen(word);
    int text_len = strlen(text);
   
    for(i = 0; i < text_len; i++)
    {
        if (isalpha(text[i]))
        {
            if (islower(word[i % word_len]))
            {
                index = word[i % word_len] - 'a';
            }
       
            if (isupper(word[i % word_len]))
            {
                index = word[i % word_len] - 'A';
            }
        }
       
        return index;
    }
   
    return -1;
} 

I hoped that if 'return index;' is within for loop, then
the function, if on its own, i.e. not called by any other
functions, returns different values of index with each 
iteration of the for loop. Is this correct?

I also have made another version, in which I have added 
'return index' within each if statement. But I think that
it is redundant, because the value of index is returned when
one of the if statements is true. Therefore both versions
would produce the same result, again if not called by any other
function. 

for(i = 0; i < text_len; i++)
    {
        if (islower(word[i % word_len]))
        {
            index = word[i % word_len] - 'a';
            return index;
        }
        
        if (isupper(word[i % word_len]))
        {
            index = word[i % word_len] - 'A';
            return index;
        }
    }

You say that on matter how many times the word_index is called
within new_text function, the value of index will be always the 
same. Is it because whenever the function is called, each time it 
starts the for loop from the beginning when i = 0; therefore, 
the value of index never changes. If this is correct, then I should
go back to my previous version in which the computation of index 
was within next_text function. I think only thus I can achieve a 
correct value, although it creates a busy appearance of the next_text
function. 

[toc] | [prev] | [next] | [standalone]


#74417

FromBarry Schwarz <schwarzb@dqel.com>
Date2015-10-31 03:09 -0700
Message-ID<1g493blqjuvobjgil3lkqc9eobv0rk8ls8@4ax.com>
In reply to#74405
On Fri, 30 Oct 2015 23:58:35 -0700 (PDT), Alla _
<modelling.data@gmail.com> wrote:

>
>> 
>>      It did allow the for loop to iterate.  The function now returns
>> after processing the first alphabetic character.  The function still
>> returns the same value regardless of how many times you call it in the
>> function new_text.
>> 
>
>I see what you mean, but I am not sure I understand the reason
>correctly. Please, take a look at my explanation below (the very
>last paragraph).
>
>I assume you are talking about the following version of the 
>word_index function:
>
>
>int word_index(const char *text, const char *word)
>{
>    int i, index;
>    int word_len = strlen(word);
>    int text_len = strlen(text);
>   
>    for(i = 0; i < text_len; i++)
>    {
>        if (isalpha(text[i]))
>        {
>            if (islower(word[i % word_len]))
>            {
>                index = word[i % word_len] - 'a';
>            }
>       
>            if (isupper(word[i % word_len]))
>            {
>                index = word[i % word_len] - 'A';
>            }
>        }
>       
>        return index;
>    }
>   
>    return -1;
>} 
>
>I hoped that if 'return index;' is within for loop, then
>the function, if on its own, i.e. not called by any other
>functions, returns different values of index with each 
>iteration of the for loop. Is this correct?

Once the function returns, the loop is no longer iterating.  When the
function is called a subsequent time, there is no residue from the
previous call.  Each time the function is entered, it is as if it is
the first time.  Since the arguments never change, every time you call
this function it performs the exact same processing.

>I also have made another version, in which I have added 
>'return index' within each if statement. But I think that
>it is redundant, because the value of index is returned when
>one of the if statements is true. Therefore both versions
>would produce the same result, again if not called by any other
>function. 
>
>for(i = 0; i < text_len; i++)
>    {
>        if (islower(word[i % word_len]))
>        {
>            index = word[i % word_len] - 'a';
>            return index;
>        }
>        
>        if (isupper(word[i % word_len]))
>        {
>            index = word[i % word_len] - 'A';
>            return index;
>        }
>    }
>
>You say that on matter how many times the word_index is called
>within new_text function, the value of index will be always the 
>same. Is it because whenever the function is called, each time it 
>starts the for loop from the beginning when i = 0; therefore, 
>the value of index never changes. If this is correct, then I should
>go back to my previous version in which the computation of index 
>was within next_text function. I think only thus I can achieve a 
>correct value, although it creates a busy appearance of the next_text
>function. 

If you want word_index (which appears to be a poor choice of a name)
to process different data, you have to change at least one of the
arguments when you call it multiple times.

-- 
Remove del for email

[toc] | [prev] | [next] | [standalone]


#74419

FromAlla _ <modelling.data@gmail.com>
Date2015-10-31 03:47 -0700
Message-ID<7c9e44f1-002f-4015-8261-b0a6cfb2086f@googlegroups.com>
In reply to#74417
yes, I understood that eventually :)
Though I have not yet figured out how to create a 
separate function for index, I have solved the issue
within next_text function, and thus the program
works as it should. I am glad this is done.

// Program to convert a text based on vigenere cipher
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int is_word(const char *string);
int word_index(const char *text, const char *word);
void new_text(char text[], char key_word[]);

int main(int argc, char *argv[])
{
    char message[80];
    // Check if there are two arguments, and argv[1] is a word
    if (argc == 2 && is_word(argv[1]))
    {
        char *key = argv[1];
        // Prompt the user for a text message
        if (fgets (message, sizeof message, stdin) != NULL)
        {
            new_text(message, key); 
        }
    }
    // Returns one if the user has not provided correct arguments
    else
    {
        printf("Please, provide one argument - a word\n");
        return 1; 
    }

    return 0;
}
 
 // Function to check if the key_word is a word           
int is_word(const char *string)
{
    int word = 0;
    // Check if the word has been entered 
    if (string != NULL)
    {
        word = 1;
        // Check if each character in the word is an alphabetical one
        while(word != 0 && *string != '\0')
        {
            word = (isalpha(*string++));
        }
    }
    
    return word;
}

// Function to create ciphered text
void new_text(char text[], char key_word[])
{
    int len = strlen(text);
    int key_len = strlen(key_word);
    int key_index;
    int j = 0;
    char new_letter;
        
    for (int i = 0; i < len; i++)
    {
        int letter = text[i];
        
        if (islower (letter))
        {
            // Compute the index based on the case of the letter
            if (islower(key_word[j % key_len]))
                    
                key_index = key_word[j % key_len] - 'a';
                
            if (isupper(key_word[j % key_len]))
                    
                key_index = key_word[j % key_len] - 'A';
                
            new_letter = (letter - 'a' + key_index) % 26 + 'a';
            
            printf("%c", new_letter);
            j++;
        }
            
        else if (isupper (letter))
        {
             // Compute the index based on the case of the letter
            if (islower(key_word[j % key_len]))
                    
                key_index = key_word[j % key_len] - 'a';
                
            if (isupper(key_word[j % key_len]))
                    
                key_index = key_word[j % key_len] - 'A';
                
            new_letter = (letter - 'A' + key_index) % 26 + 'A';
            
            printf("%c", new_letter);
            j++;
        }
        
        // All non-alphabetical characters are printed out unchanged
        else
            printf("%c", text[i]);
        
    }
}

[toc] | [prev] | [next] | [standalone]


#74469

FromBarry Schwarz <schwarzb@dqel.com>
Date2015-10-31 11:42 -0700
Message-ID<p41a3b5gkqn6tf1iasn1losn0v1pk30t2g@4ax.com>
In reply to#74419
On Sat, 31 Oct 2015 03:47:22 -0700 (PDT), Alla _
<modelling.data@gmail.com> wrote:

>yes, I understood that eventually :)
>Though I have not yet figured out how to create a 
>separate function for index, I have solved the issue

One way to make a separate function to calculate index is to call it
from new_text with two character (not string or pointer) arguments:
     the current character in key_word (key_word[j%key_len])
     the current character in text (text[i] or letter)

You could even eliminate much of the repetitive code by applying
tolower to both arguments as part of the function call.

>within next_text function, and thus the program
>works as it should. I am glad this is done.
>
>// Program to convert a text based on vigenere cipher
>#include <stdio.h>
>#include <string.h>
>#include <ctype.h>
>
>int is_word(const char *string);
>int word_index(const char *text, const char *word);
>void new_text(char text[], char key_word[]);
>
>int main(int argc, char *argv[])
>{
>    char message[80];
>    // Check if there are two arguments, and argv[1] is a word
>    if (argc == 2 && is_word(argv[1]))
>    {
>        char *key = argv[1];
>        // Prompt the user for a text message
>        if (fgets (message, sizeof message, stdin) != NULL)
>        {
>            new_text(message, key); 

You pass argv[1] to is_word.  But then for some reason you feel the
need to copy argv[1] (the pointer value, not the string it points to)
to key and pass an unchanged key to new_text repeatedly.  Is there a
reason you felt you needed an extra copy of the pointer value?

>        }
>    }
>    // Returns one if the user has not provided correct arguments
>    else
>    {
>        printf("Please, provide one argument - a word\n");
>        return 1; 

Since bad habits can be hard to break, I suggest you develop the habit
of including stdlib.h and returning EXIT_FAILURE.  In addition to
making your code more portable, it will insulate you against potential
problems if you change operating systems or compilers.

>    }
>
>    return 0;
>}
> 
> // Function to check if the key_word is a word           
>int is_word(const char *string)
>{
>    int word = 0;
>    // Check if the word has been entered 
>    if (string != NULL)

You call this function to test if the string pointed to by argv[1] is
a word.  Since you have already verified in main that argc is 2,
argv[2] will be NULL but argv[1] cannot be.  This test will always
evaluate to true.

A more meaningful test would be to verify that the original string[0]
is not '\0'.  As currently written, if argv[1] points to an empty
string, the function returns 1 but I don't think an empty string
should be considered a word.

>    {
>        word = 1;
>        // Check if each character in the word is an alphabetical one
>        while(word != 0 && *string != '\0')
>        {
>            word = (isalpha(*string++));
>        }
>    }
>    
>    return word;
>}
>// Function to create ciphered text
>void new_text(char text[], char key_word[])
>{
>    int len = strlen(text);
>    int key_len = strlen(key_word);
>    int key_index;
>    int j = 0;
>    char new_letter;
>        
>    for (int i = 0; i < len; i++)
>    {
>        int letter = text[i];
>        
>        if (islower (letter))
>        {
>            // Compute the index based on the case of the letter
>            if (islower(key_word[j % key_len]))
>                    
>                key_index = key_word[j % key_len] - 'a';
>                
>            if (isupper(key_word[j % key_len]))
>                    
>                key_index = key_word[j % key_len] - 'A';
>                
>            new_letter = (letter - 'a' + key_index) % 26 + 'a';
>            
>            printf("%c", new_letter);
>            j++;
>        }
>            
>        else if (isupper (letter))
>        {
>             // Compute the index based on the case of the letter
>            if (islower(key_word[j % key_len]))
>                    
>                key_index = key_word[j % key_len] - 'a';
>                
>            if (isupper(key_word[j % key_len]))
>                    
>                key_index = key_word[j % key_len] - 'A';
>                
>            new_letter = (letter - 'A' + key_index) % 26 + 'A';
>            
>            printf("%c", new_letter);
>            j++;
>        }
>        
>        // All non-alphabetical characters are printed out unchanged
>        else
>            printf("%c", text[i]);
>        
>    }
>}

-- 
Remove del for email

[toc] | [prev] | [next] | [standalone]


#74356

FromBarry Schwarz <schwarzb@dqel.com>
Date2015-10-30 12:04 -0700
Message-ID<j4f73bhrcm1a25fs04usomvojppu247tkv@4ax.com>
In reply to#74350
On Fri, 30 Oct 2015 10:53:47 -0700 (PDT), Alla _
<modelling.data@gmail.com> wrote:

>void new_text(char text[], char key_word[])
>{
>    int i;
>    int len = strlen(text);
>    char new_letter;
>    
>    for (i = 0; i < len; i++)
>    {
>        int letter = text[i];
>            
>        if (islower (letter))
>        {
>            new_letter = (letter - 'a' + word_index(text, key_word)) % 26 + 'a';

Since key_word does not change, word_index will always return the same
value.  Calling it in this loop is a waste of time.  Call it once
before the loop and save the value.

Have you considered what happens when word_index returns -1 (consider
the border conditions when letter is 'a' or 'z')?

>            putchar(new_letter);
>        }
>            
>        else if (isupper (letter))
>        {
>            new_letter = (letter - 'A' + word_index(text, key_word)) % 26 + 'A';
>            putchar(new_letter);
>        }
>        
>        else
>            putchar(text[i]);
>    }
>    printf ("\n");
>}

-- 
Remove del for email

[toc] | [prev] | [next] | [standalone]


#74357

FromAlla _ <modelling.data@gmail.com>
Date2015-10-30 12:10 -0700
Message-ID<b1d27758-f3db-49ac-b08e-149f0d78385f@googlegroups.com>
In reply to#74350
On Friday, October 30, 2015 at 9:53:57 PM UTC+4, Alla _ wrote:
> did not correct putchar function, my mistake. now it is correct.
> 
> //** Program to cipher the text based on vigenere algorithm **/
> 
> #include <stdio.h>
> #include <string.h>
> #include <ctype.h>
> 
> int is_word(const char *string);
> int word_index(const char *text, const char *word);
> void new_text(char text[], char key_word[]);
> 
> int main (int argc, char *argv[])
> {
>     char message[80];
>     
>     if (argc == 2 && is_word(argv[1]))
>     {
>         char *key = argv[1];
>         
>         printf("Please, enter your message: \n");
>         
>         if (fgets (message, sizeof message, stdin) != NULL)
>         {
>             new_text(message, key);
>         }
>     }
>     
>     else
>     {
>         printf("\a");
>         printf("Please, provide one argument - a word\n");
>         return 1;
>     }
> 
>     return 0;
> }
>             
> int is_word(const char *string)
> {
>     int word = 0;
>     
>     if (string != NULL)
>     {
>         word = 1;
>         
>         while(word != 0 && *string != '\0')
>         {
>             word = (isalpha(*string++)); /** the pointer refers to the address of the first
>                                           character, hence *string++ is used **/
>                                          /** if the string is not an alphabetical one, then
>                                           isalpha returns 0, the while loop terminates, and 
>                                           the function returns 0 as the value of the word**/
>         }
>     }
>     
>     return word;
> }
> 
> int word_index(const char *text, const char *word)
> {
>     int i, index;
>     int word_len = strlen(word);
>     int text_len = strlen(text);
>     
>     for(i = 0; i < text_len; i++)
>     {
>        if (isalpha(text[i])  // once learned, I deleted isalpha even here
                                  where it is essential; now it's back
         { 
           if (islower(word[i % word_len]))
>          {
>             index = word[i % word_len] - 'a';
>             return index;
>          }
>         
>          else if (isupper(word[i % word_len]))
>          {
>             index = word[i % word_len] - 'A';
>             return index;
>          }
>          else
>             return -1;
        }
>     }
>     
>     return -1;
> }
> 
> void new_text(char text[], char key_word[])
> {
>     int i;
>     int len = strlen(text);
>     char new_letter;
>     
>     for (i = 0; i < len; i++)
>     {
>         int letter = text[i];
>             
>         if (islower (letter))
>         {
>             new_letter = (letter - 'a' + word_index(text, key_word)) % 26 + 'a';
>             putchar(new_letter);
>         }
>             
>         else if (isupper (letter))
>         {
>             new_letter = (letter - 'A' + word_index(text, key_word)) % 26 + 'A';
>             putchar(new_letter);
>         }
>         
>         else
>             putchar(text[i]);
>     }
>     printf ("\n");
> }

[toc] | [prev] | [next] | [standalone]


#74359

From"Osmium" <r124c4u102@comcast.net>
Date2015-10-30 13:17 -0600
Message-ID<d9hu1oF74h8U1@mid.individual.net>
In reply to#74314
"Alla _" wrote:

<snippage>


> int is_word(const char *string)
> {
>    int word = 0;
>
>    if (string != NULL)
>    {
>        word = 1;
>
>        while(word != 0 && *string != '\0')
>        {
>            word = (isalpha(*string++)); /** the pointer refers to the 
> address of the first
>                                          character, hence *string++ is 
> used **/
>                                         /** if the string is not an 
> alphabetical one, then
>                                          isalpha returns 0, the while loop 
> terminates, and
>                                          the function returns 0 as the 
> value of the word**/
>        }
>    }
>
>    return word;
> }

You should return to the caller as soon as you find a problem, not keep 
fooling around with the datum like a cat with a mouse. I think this is your 
first real problem, but I didn't test it.

I note you have some of the most time consuming and attention clouding 
comments that I have seen in a long time.  If you tock all your comments 
out, I would like your program better.

If you take out the call to this function, one gets what looks like it might 
be enciphered output.:) 

[toc] | [prev] | [next] | [standalone]


#74406

FromAlla _ <modelling.data@gmail.com>
Date2015-10-31 00:04 -0700
Message-ID<bedd8aa1-a695-4cf8-a1e6-b9693172f5af@googlegroups.com>
In reply to#74359
On Friday, October 30, 2015 at 11:17:23 PM UTC+4, Osmium wrote:
> "Alla _" wrote:
> 
> <snippage>
> 
> 
> > int is_word(const char *string)
> > {
> >    int word = 0;
> >
> >    if (string != NULL)
> >    {
> >        word = 1;
> >
> >        while(word != 0 && *string != '\0')
> >        {
> >            word = (isalpha(*string++)); /** the pointer refers to the 
> > address of the first
> >                                          character, hence *string++ is 
> > used **/
> >                                         /** if the string is not an 
> > alphabetical one, then
> >                                          isalpha returns 0, the while loop 
> > terminates, and
> >                                          the function returns 0 as the 
> > value of the word**/
> >        }
> >    }
> >
> >    return word;
> > }
> 
> You should return to the caller as soon as you find a problem, not keep 
> fooling around with the datum like a cat with a mouse. I think this is your 
> first real problem, but I didn't test it.
> 
> I note you have some of the most time consuming and attention clouding 
> comments that I have seen in a long time.  If you tock all your comments 
> out, I would like your program better.
> 
> If you take out the call to this function, one gets what looks like it might 
> be enciphered output.:)

Even though I am a thicko, I still would dare to disagree. 
This is not a problem of this function. The main part works 
the way it should work. 

[toc] | [prev] | [next] | [standalone]


#74407

FromAlla _ <modelling.data@gmail.com>
Date2015-10-31 00:25 -0700
Message-ID<279a12c9-4121-4c50-9e31-7803dde86478@googlegroups.com>
In reply to#74406
> > If you take out the call to this function, one gets what looks like it might 
> > be enciphered output.:)
> 
> Even though I am a thicko (I know that in BE it means unintelligent
person, which is what I mean; not sure about AE), I still would dare to 
disagree. 
> This is not a problem of this function. The main part works 
> the way it should work.

[toc] | [prev] | [next] | [standalone]


#74360

FromKeith Thompson <kst-u@mib.org>
Date2015-10-30 12:34 -0700
Message-ID<lna8r09mld.fsf@kst-u.example.com>
In reply to#74314
Alla _ <modelling.data@gmail.com> writes:
[...]
> int main (int argc, char *argv[])
> {
>     char message[80];
>     
>     if (argc == 2)
>     {
>         if (is_word(argv[1])) /** if is_word returns 1, i.e. is TRUE, then
>                                do the following; if is_word returns zero, then go
>                                to else part **/
>         {
>             char *key = argv[1];
>         
>             printf("Please, enter your message: \n");
>         
>             if (fgets (message, sizeof message, stdin) != NULL)
>             {
>                 new_text(message, key);
>             }
>         }
>     }
>     
>     else
>     {
>         printf("\a");
>         printf("Please, provide one argument - a word\n");
>         return 1;
>     }
>
>     return 0;
> }
[snip]

If fgets() succeeds (and returns a non-NULL value), you call new_text().

If it fails (either because of an input error or because the user
provided no input), you do nothing.

Is that what you want?

You print an error message if the user didn't provide a proper
command-line argument.  Do you also want to print an error message if
the user didn't enter a message?

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#74402

FromAlla _ <modelling.data@gmail.com>
Date2015-10-30 21:49 -0700
Message-ID<2990d9c4-38b6-41f4-8504-bc95e8eb16ea@googlegroups.com>
In reply to#74360
> 
> If fgets() succeeds (and returns a non-NULL value), you call new_text().
> 
> If it fails (either because of an input error or because the user
> provided no input), you do nothing.
> 
> Is that what you want?
> 
> You print an error message if the user didn't provide a proper
> command-line argument.  Do you also want to print an error message if
> the user didn't enter a message?
> 
Everything in the main function, especially after your suggestions
on efficiency, is now the way it should be. Even before the main one
worked the way it was set in the task.
Below I am posting the first program before I have made changes to it,
and this one produces the correct result only for the first word in the
entered message; but fails to compute correctly the middle word, because
index is wrapping around in a wrong way, not based on alphabetical 
characters of the entered message.

Should be this:

 world, say hello!

(bazba  zba zbazb) // this is how key word should be "distributed"
                      under the hood

the program should convert this phrase to

'xoqmd, rby gflkp!'

But the result is:
world, say hello!
xoqmd, szz gflkp!

This is why I started to correct my index, because the problem is 
in the index. Gradually I came to the version which I repost below also.

Original one:
//** Program to cipher the text based on vigenere algorithm **/

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

void new_text(char text[], char key_word[]);

int main (int argc, char *argv[])
{
    char message[80];
    
    if (argc == 1 || argc > 2)
    {
        printf("\a");
        printf("Please, provide one argument - a word\n");
        return 1;
    }
    
    else
    {
        char *key = argv[1];
        
        printf("Please, enter your message: \n");
        
        if (fgets (message, sizeof message, stdin) != NULL)
            new_text(message, key);
    }
    
    return 0;
}

void new_text(char text[], char key_word[])
{
    int i;
    int len = strlen(text);
    int key_len = strlen(key_word);
    int key_index;
    char new_letter;
    
    for (i = 0; i < len; i++)
    {        
        if (isalpha (text[i]))
        {
            int letter = text[i];
            
            if (islower (letter))
            {
                if (islower(key_word[i % key_len]))
                    
                    key_index = key_word[i % key_len] - 'a';
                
                if (isupper(key_word[i % key_len]))
                    
                        key_index = key_word[i % key_len] - 'A';
                
                new_letter = (letter - 'a' + key_index) % 26 + 'a';
            }
            
            if (isupper (letter))
            {   
                if (islower(key_word[i % key_len]))
                    
                    key_index = key_word[i % key_len] - 'a';
                
                if (isupper(key_word[i % key_len]))
                    
                    key_index = key_word[i % key_len] - 'A';
                
                new_letter = (letter - 'A' + key_index) % 26 + 'A';
            }
            
            printf("%c", new_letter);
        }
        
        else
            printf("%c", text[i]);
        
    }
    printf ("\n");
}

New version, that is supposed to compute correct index, but fails:

//** Program to cipher the text based on vigenere algorithm **/

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

int is_word(const char *string);
int word_index(const char *text, const char *word);
void new_text(char text[], char key_word[]);

int main (int argc, char *argv[])
{
    char message[80];
    
    if (argc == 2 && is_word(argv[1]))
    {
        char *key = argv[1];
        
        printf("Please, enter your message: \n");
        
        if (fgets (message, sizeof message, stdin) != NULL)
        {
            new_text(message, key);
        }
    }
    
    else
    {
        printf("\a");
        printf("Please, provide one argument - a word\n");
        return 1;
    }

    return 0;
}
            
int is_word(const char *string)
{
    int word = 0;
    
    if (string != NULL)
    {
        word = 1;
        
        while(word != 0 && *string != '\0')
        {
            word = (isalpha(*string++));
        }
    }
    
    return word;
}

int word_index(const char *text, const char *word)
{
    int i, index;
    int word_len = strlen(word);
    int text_len = strlen(text);
    
    for(i = 0; i < text_len; i++)
    {
        if (islower(word[i % word_len]))
        {
            index = word[i % word_len] - 'a';
            return index;
        }
        
        if (isupper(word[i % word_len]))
        {
            index = word[i % word_len] - 'A';
            return index;
        }
    }
    
    return -1;
}

void new_text(char text[], char key_word[])
{
    int i;
    int len = strlen(text);
    char new_letter;
    
    for (i = 0; i < len; i++)
    {
        int letter = text[i];
            
        if (islower (letter))
        {
            new_letter = (letter - 'a' + word_index(text, key_word)) % 26 + 'a';
            putchar(new_letter);
        }
            
        else if (isupper (letter))
        {
            new_letter = (letter - 'A' + word_index(text, key_word)) % 26 + 'A';
            putchar(new_letter);
        }
        
        else
            putchar(text[i]);
    }
    printf ("\n");
}

[toc] | [prev] | [next] | [standalone]


#74485

FromKeith Thompson <kst-u@mib.org>
Date2015-10-31 13:18 -0700
Message-ID<lnk2q294hm.fsf@kst-u.example.com>
In reply to#74402
Alla _ <modelling.data@gmail.com> writes:
>> If fgets() succeeds (and returns a non-NULL value), you call new_text().
>> 
>> If it fails (either because of an input error or because the user
>> provided no input), you do nothing.
>> 
>> Is that what you want?
>> 
>> You print an error message if the user didn't provide a proper
>> command-line argument.  Do you also want to print an error message if
>> the user didn't enter a message?

I wrote the above text.  You didn't include the attribution line that
would have indicated that.  (I understand that Google Groups makes it
difficult to trim quoted text properly.)

> Everything in the main function, especially after your suggestions
> on efficiency, is now the way it should be. Even before the main one
> worked the way it was set in the task.
> Below I am posting the first program before I have made changes to it,
> and this one produces the correct result only for the first word in the
> entered message; but fails to compute correctly the middle word, because
> index is wrapping around in a wrong way, not based on alphabetical 
> characters of the entered message.

In both versions, you do nothing if printf returns NULL (i.e., if the
user doesn't provide any input).  Is that your intent?  (In my opinion
it would be appropriate to print an error message.)

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#74408

FromAlla _ <modelling.data@gmail.com>
Date2015-10-31 00:29 -0700
Message-ID<69f82972-333b-482e-9f03-33b4a4b3bef4@googlegroups.com>
In reply to#74314
I hope to solve this 'wrapping around index' issue.
I have tried this version, which should have computed 
the correct index, but did not.

The result should be with a 'baz' as second argc:
world, say hello!
xoqmd, rby gflkp!

But this version produces:
xpsme, tbz ifmmp!

Only first and forth letters are at their places (most likely
accidentally).
What do you think?
//** Program to cipher the text based on vigenere algorithm **/

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

int is_word(const char *string);
int word_index(const char *text, const char *word);
void new_text(char text[], char key_word[]);

int main (int argc, char *argv[])
{
    char message[80];
    
    if (argc == 2 && is_word(argv[1]))
    {
        char *key = argv[1];
        
        printf("Please, enter your message: \n");
        
        if (fgets (message, sizeof message, stdin) != NULL)
        {
            new_text(message, key);
        }
    }
    
    else
    {
        printf("Please, provide one argument - a word\n");
        return 1;
    }

    return 0;
}
            
int is_word(const char *string)
{
    int word = 0;
    
    if (string != NULL)
    {
        word = 1;
        
        while(word != 0 && *string != '\0')
        {
            word = (isalpha(*string++));
        }
    }
    
    return word;
}

void new_text(char text[], char key_word[])
{
    int len = strlen(text);
    int key_len = strlen(key_word);
    int key_index;
    char new_letter;
    
    for (int i = 0; i < len; i++)
    {
        int letter = text[i];
        int j = 0;
        
        if (islower (letter))
        {
            if (islower(key_word[j % key_len]))
                    
                key_index = key_word[j % key_len] - 'a';
                
            if (isupper(key_word[j % key_len]))
                    
                key_index = key_word[j % key_len] - 'A';
                
            new_letter = (letter - 'a' + key_index) % 26 + 'a';
            
            printf("%c", new_letter);
            j++;
        }
            
        else if (isupper (letter))
        {
            if (islower(key_word[j % key_len]))
                    
                key_index = key_word[j % key_len] - 'a';
                
            if (isupper(key_word[j % key_len]))
                    
                key_index = key_word[j % key_len] - 'A';
                
            new_letter = (letter - 'A' + key_index) % 26 + 'A';
            
            printf("%c", new_letter);
            j++;
        }
        
        else
            printf("%c", text[i]);
        
    }
    printf ("\n");
}

[toc] | [prev] | [next] | [standalone]


#74410

FromAlla _ <modelling.data@gmail.com>
Date2015-10-31 01:12 -0700
Message-ID<1e1a0897-ba67-47b3-b778-0bc03f8b098b@googlegroups.com>
In reply to#74314
Thank you Everyone very much. 
I have solved the problem with this program - 
the program now computes the index correctly, and 
returns a correct result :)

[toc] | [prev] | [next] | [standalone]


#74426

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-10-31 12:24 +0000
Message-ID<87oaffb4z9.fsf@bsb.me.uk>
In reply to#74410
Alla _ <modelling.data@gmail.com> writes:

> Thank you Everyone very much. 
> I have solved the problem with this program - 
> the program now computes the index correctly, and 
> returns a correct result :)

As you were writing it I thought it looked fun so I had a go too.  For
what it's worth, here's my version.  I make it read input rather than
process a string and it's not intended to use only the features you've
learned about so far because I don't know what they are.

I don't suggest it's a model to copy -- not at all -- but it might give
you some things to thin over.  It never hurts to read code, even bad
code.

Spoiler space, in case anyone does not want to see it yet:
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

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

const char letters[] = "abcdefghijklmnopqrstuvwxyz";

int letter_number(char ch)
{
     const char *found = strchr(letters, tolower((unsigned char)ch));
     return found ? found - letters : -1;
}

bool can_set_offsets(int *offsets, const char *key_word)
{
     while (*key_word)
          if ((*offsets++ = letter_number(*key_word++)) < 0)
               return false;
     return true;
}

char rotate(char ch, int offset)
{
     int lno = letter_number(ch);
     if (lno < 0)
          return ch;
     int rotated_char = letters[(lno + offset) % (sizeof letters - 1)];
     return islower((unsigned char)ch) ? rotated_char : toupper(rotated_char);
}

int main(int argc, char **argv)
{
     if (argc != 2) {
          fprintf(stderr, "Provide a%s key word argument.\n",
                  argc > 2 ? " single" : "");
          return EXIT_FAILURE;
     }
     size_t key_len = strlen(argv[1]);
     if (key_len == 0) {
          fprintf(stderr, "Key word must not be empty.\n");
          return EXIT_FAILURE;
     }
     int key_offsets[key_len];
     if (!can_set_offsets(key_offsets, argv[1])) {
          fprintf(stderr, "Key word must contain only letters.\n");
          return EXIT_FAILURE;
     }
     int n = 0, c;
     while ((c = getchar()) != EOF) {
          putchar(rotate(c, key_offsets[n % key_len]));
          n += isalpha(c);
     }
     return EXIT_SUCCESS;
}


-- 
Ben.

[toc] | [prev] | [next] | [standalone]


Page 3 of 4 — ← Prev page 1 2 [3] 4  Next page →

Back to top | Article view | comp.lang.c


csiph-web