-2

I have an array formed from a text file imported by stdin.

The text file looks like this:

"Name"
"Number"
"Name"
"Number"
...

The entire code:

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



int main(int argc, char** argv)
{
    //number of arguments

    if (argc > 2)
    {
        fprintf(stderr, "Too many arguments\n");
        return 1;
    }

    //check argument 1

    {
        if (argc == 2)
        {
            unsigned i = 0;

            while (i < strlen(argv[1]))
            {
                if ((isdigit(argv[1][i])) == 0)
                {
                    fprintf(stderr, "Enter a number\n");
                    return 1;
                }
                i++;
            }
        }
        else
        {
            fprintf(stderr, "argument\n");
            return -1;
        }
    }


    //find \n and separate

    int g = 0;
    int c = 0;
    char buffer[102];
    char people[42][102];
    char numbers[42][102];

    while (fgets(buffer, sizeof buffer, stdin) != NULL)
    {
        if (g % 2 == 0)
        {
            strcpy(people[c], buffer);
            //printf("%s", people[c]);
        }

        if (g % 2 == 1)
        {
            strcpy(numbers[c], buffer);
            c++;
        }
        g++;

    }

    //convert and remove \n

    char conv_people[42][102];
    for (int i = 0; i < c; i++)
    {
        for (unsigned j = 0; j < strlen(people[i]); j++)
        {
            if (islower(people[i][j]) == 0 && people[i][j] != ' ' && people[i][j] != '.')
            {
                if (people[i][j] == '\n')
                {
                    conv_people[i][j] = '\0';
                }
                people[i][j] = conv_people[i][j] + 32;
            }

        }
    }

    //covert to numbers

    char conv[42][102];
    for (int i = 0; i < c; i++)
    {
        for (unsigned j = 0; j < strlen(people[i]); j++)
        {
            if (conv_people[i][j] == ' ' || conv_people[i][i] == '.' || conv_people[i][i] == '\n' || conv_people[i][i] == '\0')
            {
                conv[i][j] = '0';
            }
            if (conv_people[i][j] == 'a' || conv_people[i][j] == 'b' || conv_people[i][j] == 'c')
            {
                conv[i][j] = '2';
            }
            if (conv_people[i][j] == 'd' || conv_people[i][j] == 'e' || conv_people[i][j] == 'f')
            {
                conv[i][j] = '3';
            }
            if (conv_people[i][j] == 'g' || conv_people[i][j] == 'h' || conv_people[i][j] == 'i')
            {
                conv[i][j] = '4';
            }
            if (conv_people[i][j] == 'j' || conv_people[i][j] == 'k' || conv_people[i][j] == 'l')
            {
                conv[i][j] = '5';
            }
            if (conv_people[i][j] == 'm' || conv_people[i][j] == 'n' || conv_people[i][j] == 'o')
            {
                conv[i][j] = '6';
            }
            if (conv_people[i][j] == 'p' || conv_people[i][j] == 'q' || conv_people[i][j] == 'r' || conv_people[i][j] == 's')
            {
                conv[i][j] = '7';
            }
            if (conv_people[i][j] == 't' || conv_people[i][j] == 'u' || conv_people[i][j] == 'v')
            {
                conv[i][j] = '8';
            }
            if (conv_people[i][j] == 'w' || conv_people[i][j] == 'x' || conv_people[i][j] == 'y' || conv_people[i][j] == 'z')
            {
                conv[i][j] = '9';
            }
        }
    }

    //compare

    int i = 0;
    while (i < c)
    {
        if (strstr(conv[i], argv[1]) != NULL)
            printf("%s, %s", people[i], numbers[i]);
        if (strstr(numbers[i], argv[1]) != NULL)
            printf("%s, %s", people[i], numbers[i]);
        i++;
    }
    return 0;
}

The program takes a list of people and their phone numbers and searches it using argv[1] The output always omits the first capital letter in each word So if the file contains a name like: Barrack Obama the program returns arrack bama The numbers and converted names are working fine

I didn't want to post the whole thing because it's extremely ugly.

5
  • 2
    What is conv[i]? Is it a typo? If so, is this your actual code? Without a Minimal Reproducible Example it's not easy to say what is wrong. Nov 13, 2019 at 21:00
  • Does the file really have quotes around each line?
    – Barmar
    Nov 13, 2019 at 21:04
  • I'll bet the file has CRLF line breaks. Use dos2unix to fix it.
    – Barmar
    Nov 13, 2019 at 21:05
  • It's a snippet of code. The code inbetween doesn't do anything with the array in question, but I'll edit the whole thing in, conv[i] is an array of letter converted to numbers and it's a really ugly function... Nov 13, 2019 at 21:07
  • I don't see main here.
    – S.S. Anne
    Nov 13, 2019 at 21:10

1 Answer 1

1

I've run the code and John is output as Éohn. It likely comes from

people[i][j] = conv_people[i][j] + 32;

because you never set any values in conv_people[i] except a terminator.

If I add this first line in the loop

strcpy(conv_people[i], people[i]);

then is outputs

john

with a lower case initial letter.

Aside: it is safer and convenient to use

people[i][j] = tolower(conv_people[i][j]);

which doesn't even need to be tested to see if an uppercase letter was passed.

Not the answer you're looking for? Browse other questions tagged or ask your own question.