Question

Create a C project named login_l06t1. Write and test a function strnclean that takes two strings...

Create a C project named login_l06t1. Write and test a function strnclean that takes two strings as parameters, target and source. Using the ctype library, copy only the alphabetic characters from source to target, and make the characters lower case. Ex:

source: David Brown!
target: davidbrown

Prototype: void strnclean(char *target, const char *source);

How do i do this using the C type library below!

int isalnum(int c)

Returns non-zero (true) if c is an alphanumeric character, zero otherwise.

int isalpha(int c)

Returns non-zero (true) if c is an alphabetic character, zero otherwise.

int isdigit(int c)

Returns non-zero (true) if c is a digit character, zero otherwise.

int islower(int c)

Returns non-zero (true) if c is a lower-case alphabetic character, zero otherwise.

int ispunct(int c)

Returns non-zero (true) if c is a punctuation character, zero otherwise.

int isspace(int c)

Returns non-zero (true) if c is a space alphabetic character, zero otherwise.

int isupper(int c)

Returns non-zero (true) if c is an upper-case alphabetic character, zero otherwise.

It also provides a pair of character conversion functions:

int tolower(int c)

Returns a lower-case version of c if it is an upper-case alphabetic character.

int toupper(int c)

Returns an upper-case version of c if it is a lower-case alphabetic character.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

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

#define size sizeof(char)*100

//Prototype declaration..
void strnclean(char *target, const char *source);

int main( void )
{
    char *source = NULL;        //Source
    char *target = NULL;            //Target
    //Dynamic Memory allocation..
    target = (char *) malloc (size * sizeof (char));
    source = (char *) malloc (size * sizeof (char));

    printf("\n\n Enter the string : ");
    fgets(source, size, stdin); //Read string 1...

    //Func call to get the target..
    strnclean(target, source);
    printf("\n Source : %s" , source);
    printf("\n Target : %s\n\n" , target);

    free(source);
    free(target);
return 0;
}

/**
* Function akes two strings as parameters, target and sourcei..
* Using the ctype library, copy only the alphabetic characters
   from source to target, and make the characters lower case
*/

void strnclean(char *target, const char *source)
{
    int i = 0;
    int j = 0;
    int count =0;       //Counts the length of continuos mnatching chars
    char c;
    //Travesing the source char by char..
    for (i = 0; *(source + i) != '\0' ; i++) {
      
        c = (*(source + i));

        //Returns non-zero (true) if c is a digit character, zero else.
        if(isdigit(c))
            continue;
        //Returns non-zero (true) if c is punctuation character, else zero.
        else if(ispunct(c))
            continue;
        //Returns non-zero (true) if c is space alphabetic character,zero else.
        else if( isspace(c))
            continue;
        //Returns non-zero (true) if c is an alphabetic character, zero else.
        else if(isalpha(c)) {
            //Returns non-zero (true) if c is a lower-case alphabetic character,            //zero else.
            if(islower(c)) {
                (*(target+j)) = c;      //Assigning to target
                j++;                    //Increment j value..
            }
            else {
                //Returns non-zero (true) if c is an upper-case \
                alphabetic character, zero else.
                if(isupper(c)) {
                    //Returns a lower-case version of c if it is an \
                    upper-case alphabetic character.
                    (*(target+j)) = (tolower(c));       //Assigning to target
                    j++;
                }
            }
        }
        //Returns non-zero (true) if c is an alphanumeric character,zero else.
        else if(isalnum(c))
            continue;
    }
}

--------------------------------------

Code Screenshot:

#include-stdio.h> #include-string.h> #include<stdboot.h> #include-stdlib.h> #include<ctype.h> #define size sizeof(char)*100 /

void strnclean (char *target, const char *source) int count char c //Travesing the source char by char. . for (1-0; *(source

-------------------

Output:

                   Enter the string David Brown! SourceDavid Brown! Target: davidbrown

-----------------

Please comment if you have any queries/changes on this solution.

Kindly up-vote if you are satisfied.

Add a comment
Know the answer?
Add Answer to:
Create a C project named login_l06t1. Write and test a function strnclean that takes two strings...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with this exercise. Than you for the help. Language is C++ 2 Enumeration...

    I need help with this exercise. Than you for the help. Language is C++ 2 Enumeration Data Types Reformat is the shell of a program designed to read characters and process them in the following way Lowercase character Uppercase character Digit Blank Newline Any other character Converts to uppercase and writes the character Writes the character Writes the digit Writes a blank Writes newline Does nothing / Program Reformat reads characters from file DataIn and // writes them to DataOut...

  • can you use the isspace() method in the code what do i add to the code...

    can you use the isspace() method in the code what do i add to the code if i want to make sure that extra spaces dont affect the output of the code elect sumatra medium roast VS sumatra medium roast Det dat HETTI CV Surface Providesearch coffee record description to search for at that it was not found in the file the first on has another space in it Table 8-1 Some string testing methods Method Description isalnum() Returns true...

  • In this program, you will be using C++ programming constructs, such as overloaded functions. Write a...

    In this program, you will be using C++ programming constructs, such as overloaded functions. Write a program that requests 3 integers from the user and displays the largest one entered. Your program will then request 3 characters from the user and display the largest character entered. If the user enters a non-alphabetic character, your program will display an error message. You must fill in the body of main() to prompt the user for input, and call the overloaded function showBiggest()...

  • project-8a Write a function named count_letters that takes as a parameter a string and returns a...

    project-8a Write a function named count_letters that takes as a parameter a string and returns a dictionary that tabulates how many of each letter is in that string. The string can contain characters other than letters, but only the letters should be counted. The string could even be the empty string. Lower-case and upper-case versions of a letter should be part of the same count. The keys of the dictionary should be the upper-case letters. If a letter does not...

  • A CERTAIN program to user's password containing rules such as least n length, one uppercase, lowercase,...

    A CERTAIN program to user's password containing rules such as least n length, one uppercase, lowercase, one digit and white space is given as the code down below. So, simiiar to those rules, I need the code for the following questions post in pictures such as no more three consecutive letters of English alphabets, password should not contain User name and so on. //GOAL: To learn how to create that make strong passwords //Purpose: 1)To learn some rules that makes...

  • Write a C++ console application that reverses an array of characters, and counts the number of:...

    Write a C++ console application that reverses an array of characters, and counts the number of:           Lower case characters (islower)           Upper case characters (isupper)           Digits (isdigit)           Other (not one of previous) Create four global variables to hold these counts. Create function void count(char input[], char reverse[]) that takes as input two arrays: one that contains characters and another that will contain the reverse of that array. The function will reverse the input array and do the...

  • in c, 5. Write a function that tests whether two words are anagrams. The function returns...

    in c, 5. Write a function that tests whether two words are anagrams. The function returns 1 if the two words are anagrams, returns 0 otherwise. Two words are anagrams if they are permutations of the same letters. For example, smartest and mattress are anagrams and dumbest and stumble are not anagrams. Assume wordl and word2 are null-terminated strings containing arbitrary lower-case alphabetic letters. Hint: use an array of 26 integers to keep track of how many times each letter...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • USING JAVA Consider the following methods: StringBuilder has a method append(). If we run: StringBuilder s...

    USING JAVA Consider the following methods: StringBuilder has a method append(). If we run: StringBuilder s = new StringBuilder(); s.append("abc"); The text in the StringBuilder is now "abc" Character has static methods toUpperCase() and to LowerCase(), which convert characters to upper or lower case. If we run Character x = Character.toUpperCase('c');, x is 'C'. Character also has a static isAlphabetic() method, which returns true if a character is an alphabetic character, otherwise returns false. You will also need String's charAt()...

  • This is for C++ Write a program that reads in a sequence of characters entered by...

    This is for C++ Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along with the number of times it occured. All non-alphabetic characters must...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT