0

I'm trying to make this program such that the user could type any given string of characters, and the program would separate alphanumerical characters from the rest, print them into a second string, and finally print the final result into the screen.

I've already tried using scanf ("%[^\n]%*c", string);, but it doesn't seem to work since the size of the string is not specified beforehand, and is rather defined by STR_SIZE.

char string[STR_SIZE];

printf("please type in a string \n");
scanf("%s", string);
printf("string: \n %s \n", string);
int size = (strlen(string));
char alfanumerico[STR_SIZE];
int count = 0;
int count2 = 0;

while(count <= size)
{
    if(string[count] >= '0' && string[count] <= '9')
    {
        alfanumerico[count2] = string[count];
        count2++;
    }
    if(string[count] >= 'a' && string[count] <= 'z')
    {
        alfanumerico[count2] = string[count];
        count2++;
    }
    if(string[count] >= 'A' && string[count] <= 'Z')
    {
        alfanumerico[count2] = string[count];
        count2++;
    }
    if(string[count] ==' ')
    {
        alfanumerico[count2] = string[count];
        count2++;
    }
    count++;
}

printf("alphanumerical characters typed: \n %s \n", alfanumerico);

Given the user typed a string such as: -=-=[[][][]}}Hello 123 ```//././.

I expect the output to be: Hello 123

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
ian ferraz
  • 13
  • 2

1 Answers1

1

scanf is not the way to go, especially if your input might contain white-spaces on which scanf would stop reading more inputs and wouldn't store spaces for instance.

You should use fgets which lets you limit the input data according to the buffer this data is stored in. So something like:

fgets(string, STR_SIZE, stdin)

should work.

About the size - you should have some limitation about the maximum size of the string and then STR_SIZE should be set to this number. It should be part of your program requirements or just a size that makes sense if you're making the requirements. It must be defined before you're reading input from the user because the buffer memory is allocated before reading to it.


A comment about style, unrelated to your question - always try to decrease code duplication to 0. The line alfanumerico[count2] = string[count]; count2++; appears 4 times in your code. A more elegant minimal if statement with exactly the same functionality would be:

if ((string[count] >= '0' && string[count] <= '9') ||
    (string[count] >= 'a' && string[count] <= 'z') ||
    (string[count] >= 'A' && string[count] <= 'Z') || 
    (string[count] == ' '))
{
    alfanumerico[count2] = string[count];
    count2++;
}

and to be even more minimal:

char c = string[count];
if ((c >= '0' && c <= '9') ||
    (c >= 'a' && c <= 'z') ||
    (c >= 'A' && c <= 'Z') || 
    (c == ' '))
{
    alfanumerico[count2] = c;
    count2++;
}

It's also more readable and more maintainable - if you want to change the variable count to i you do it in one place instead of 8.

Also, always close a scope in a new line.

SHG
  • 2,516
  • 1
  • 14
  • 20
  • 1
    I’m just getting started at my computer science, so all of this is kind of new for me.... . But thanks to people like you I can move forwards even when reaching a problem for which I’m not yet prepared to solve. Anyway, thanks for answering my question and also for the tips on how making my code less complicated, truly helpful – ian ferraz Jun 06 '19 at 00:19