0

The objective of my assignment is to take in user input string and then print out the English alphabetic characters (both lower case and upper case) that the user has entered.

For example if the user inputs:D_!an!_ i12el the output would be Daniel. My approach was to loop through the input and just remove all the non alpha characters but I dont know how to.Please help with any ideas! This is what I have so far:

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

int main()
{

char my_array[100];

    printf("Enter a message: ");;

    while(strlen(gets (my_array)) == 0);
    printf(" Your message is: %s\n", my_array);

    for(int i = 0; i< strlen(my_array);i++)
    {
        if(my_array[i] < 'A' || my_array[i] > 'z')
        {
           my_array[i] = ' ';
        }
    }
    printf(" Your new message is: %s\n", my_array);
}

EDIT:I got my loop working to print out only the alpha characters but it keeps adding extra characters when i print the elements. For example D_!a_*&Ni#32el becomes DaNielASCIIV. I dont know why this is happening.

 for(int i = 0; i< 100;i++)
    {
        if (isalpha(message[i]))
        {
            putchar(message[i]);
        }
    }
3
  • You may find it conceptually easier to copy the letters to a separate array, instead of updating the input buffer in place. Commented Oct 14, 2016 at 16:02
  • If you're allowed to use <ctype.h>, then the function you're looking for is isalpha(). Otherwise, note that there are non-alpha characters between the upper case letters and the lower case letters, so you need to check the two ranges separately. Commented Oct 14, 2016 at 16:03
  • 3
    Also, never use gets(). It is unsafe, and indeed is no longer even defined by the standard (though most C libraries still provide it, for compatibility). Use fgets() instead. Commented Oct 14, 2016 at 16:04

3 Answers 3

3

Rather than trying to update the string you have, just print out a character if it's a letter.

Also, upper case and lower case characters don't immediately follow one another, so you need to check for them separately:

printf(" Your new message is: ");
for(int i = 0; i< strlen(my_array);i++)
{
    if((my_array[i] >= 'A' && my_array[i] <= 'Z') || 
       (my_array[i] >= 'z' && my_array[i] <= 'z'))
    {
       putchar(my_array[i]);
    }
}
printf("\n");

Alternetely, you could replace the above if condition with a function that checks for this:

if (isalpha(my_array[i]))

EDIT:

The reason you're now seeing extra characters is because you changed the loop to loop over the entire array instead of the length of the string. Go back to using strlen(my_array) instead of 100 and you'll be fine.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried this and almost got it working. But for some reason i get some extra characters at the end of what I need. So if I input D_!an!_ i12el it outputs DanielASCIIV.
@MasterMaq Glad I could help. Feel free to accept this answer if you found it useful.
0

Use this pattern for removing elements from an array

int i, j;

j = 0;
for (i=0;i<N;i++)
  if (good(array[i]) )
    array[j++] = array[i];

N = j;

We go through, adding everything that matches. It's efficient and in-place.

Comments

0

It might be better to loop through the input string and use strchr() to see if the characters are in the string "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz". This has the advantages of not relying on a specific ordering for of the letters of the alphabet (see here and here), and being flexible so that you can easily change the characters that you want to pick out. You could then collect the results in a string, or print the filtered characters out directly.

char my_array[100];
char filtered_array[100];
char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                  "abcdefghijklmnopqrstuvwxyz";
char *pchar;
int j = 0;

...

for (int i = 0; i < strlen(my_array); i++)
    if ((pchar = strchr(alphabet, my_array[i])) != NULL) {
        filtered_array[j] = *pchar;
        ++j;
    }
filtered_array[j] = '\0';

...

The above code collects the results in a string. Note that a null-terminator is added to the end of filtered_array[], since this character would not be copied to the new array. If you want to include spaces or hyphens in the filtered string, just add these characters to the alphabet[] string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.