2

I just started to learn loops on C language.

Write a program that checks if the input of abc alphabet is correct, the input is small letters form abc alphabet, asume that the input is orderd, if misissing some latters you should add the missing latter with capital.in the end of the input there is a $ sign.

exampls:

for the input: abcdijklmnstuvyz$ should print abcdEFGHijklmnOPQRstuvWXyz

for the input: abefghijkopqrvwxyz$ should print abCDefghijkLMNopqrSTUvwxyz

my idea was to use two arrays on for the 'a,b,c' alphabet and another after correction, here is my code:

#include <stdio.h>

int main()
{
    char student[26] = {0};
    char real[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 'c', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    char corection[26] = {0};
    int istudent = 0, ireal = 0, icorection = 0;

    for(istudent = 0; istudent<26; istudent++)//changed to < instad of <=
    scanf("%c", &student[istudent]);

    for(istudent = ireal = icorection = 0; (istudent < 26) && (ireal < 26); icorection++)
    {
        if (student[istudent] == real[ireal])
           {
           istudent++;
           ireal++;
           corection[icorection] = student[istudent];
           }
        if (student[istudent] != real[ireal])
           {
           istudent++;
           ireal++;
           corection[icorection] = (real[ireal] - 32);
           }

    }
   // print results
    int k;
    printf("printed array: \n");
    for(k=0;k<26;k++)
        printf("%c", corection[k]);
    return 0;
}

I'm tring the print the result to check if I wrote a correct code, but it doesn't show me the correct output

Community
  • 1
  • 1
Error 404
  • 427
  • 5
  • 25

2 Answers2

3
for(istudent = 0; istudent<26; istudent++)//changed to < instad of <=
    scanf("%c", &student[istudent]);

This is the problem in you code/logic. You are expecting the user to always enter 26 characters and 26 characters only.

What you need is, to stop when you see a $

so you can change your loop to

for(istudent = 0; istudent<26; istudent++){
    scanf("%c", &student[istudent]);
    if(student[istudent] == '$')
        break;
}

Or you can read the string string using

scanf("%s", student);

Secondly you are always checking both the conditions inside the for loop. You want the second condition to be skipped if first one is true.

So you can use if - else if .

Changing your code to -

else if (student[istudent] != real[ireal])

Finally inside the condition, since you are incrementing first, you need to use istudent -1 while accessing the array as

istudent++;
ireal++;
corection[icorection] = student[istudent-1];

Also you shouldn't increment istudent in the second condition, else you will skip over characters.

Here is a Demo with all the bugs fixed.

Edit:

As suggested by PeterPaulKiefer there are several improvements you can do Firstly - ireal and icorrection always change together and will always have the same value. You can eliminate one of them.

Secondly, the second condition need to be checked, if first is false, second is bound to be true. So you can just write else.

Finally you can change the increment of istudent and ireal to be done after the indexing into the array for better readability.

corection[icorection] = student[istudent];
istudent++;
Community
  • 1
  • 1
Ajay Brahmakshatriya
  • 8,993
  • 3
  • 26
  • 49
  • 2
    That's a really good answer, but I would use `icorrection` instead of `ireal`, because they are allways equal when they are used. And then use `student[istudent++]` each time and remove `istudent++` twice above. Also the second `if` is just an else. ;-) – Peter Paul Kiefer May 13 '17 at 14:42
  • 2
    @PeterPaulKiefer, I agree, a lot of things could have been changed. But my intention was to keep the code as similar to OPs code as possible, So he can see the contrast. I can add the improvements you suggested to make the code better. – Ajay Brahmakshatriya May 13 '17 at 14:46
  • 1
    My comment is no critique to your answer. I understood that, you brougth it to the point and gave the OP a correct working answer. But I was about to give an answer myself when I saw you was faster ;-). And because yours included things I didn't see, I didn't want to give my answer beside yours. So I used a comment to show my differences. Cheers... – Peter Paul Kiefer May 13 '17 at 14:55
3

First, your real alphabet is wrong, it should be:

char real[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'};

where you had c in the place of s.


For parsing the input, you want to read until the terminating character is inputed, that is the dollar sign! As a result, since you do not how many characters are going to be inputed (assuming that they are going to be less than 27 (dollar sign including), use a , like this:

char input[26] = {0};
int i = 0;
char c;
while(1)
{
    scanf("%c", &c);
    if(c != '$')
        input[i++] = c;
    else
        break;
}

where you first read a character, you check if it is the terminating sign, and if it is not, you insert it at input buffer, which stores the input.

Then you want to output the desired characters. I see that you want to construct an array holding them, so that way I followed, but note that you could simply print the character at the time.

So, use a , since you know the number of iterations, and loop over the real array. When you have a match with the input, just insert that character to output array.

When you do not, convert the corresponding character from real to capital and insert it to output array.

Of course, you would need two counters to do that, one for the real and output array, i in my code, and one for the input array, j in my code.

int j = 0;
for(i = 0; i < 26; ++i)
{
    //printf("%d %d %c %c\n", i, j, real[i], input[j]);
    if(real[i] == input[j])
        output[i] = input[j++];
    else
        output[i] = toupper(real[i]);
}

And you are done!


Full code:

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

int main(void)
{
    char input[26] = {0};
    char real[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 output[26] = {0};

    int i = 0;
    char c;
    while(1)
    {
        scanf("%c", &c);
        if(c != '$')
            input[i++] = c;
        else
            break;
    }

    int j = 0;
    for(i = 0; i < 26; ++i)
    {
        printf("%d %d %c %c\n", i, j, real[i], input[j]);
        if(real[i] == input[j])
            output[i] = input[j++];
        else
            output[i] = toupper(real[i]);
    }

    for(i = 0; i < 26; ++i)
        printf("%c", output[i]);
    printf("\n");

    return 0;
}

Output:

abcdEFGHijklmnOPQRstuvWXyz

gsamaras
  • 71,951
  • 46
  • 188
  • 305