Question

I need this in C not C++ just C SET-151 C Programming #1 Homework: 7 String...

I need this in C not C++ just C

SET-151 C Programming #1

Homework: 7 String Basics – Death by a thousand strings.

                                                                                                                                                                       

Reading(s): Chapter(s) in C Primer Plus 1-7, 9, 10

please use this format

// ------------------------------------------------------------------------------------------
// Name: Your name here
// Class: C Programming
// Abstract: Homework 1.
// ------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------
// Includes
// ------------------------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>

// ------------------------------------------------------------------------------------------
// Constants
// ------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------
// Prototypes
// ------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------
// Name: main
// Abstract: This is where the program starts
// ------------------------------------------------------------------------------------------
void main( )
{

}

YOU CAN'T USE ANY BUILT-IN STRING FUNCTIONS.

Write a function that takes strSource as a parameter. The function will return the length of the string.

Write a subroutine that takes strDestination and strSource as parameters. The subroutine will copy the source string to the destination string.

Write a function that takes strSource and chrLetterToFind as parameters. The function will return the index of the first occurrence of the letter (case sensitive) in the string searching from left to right. Return –1 if the letter is not in the string.

Write a function that takes strSource and chrLetterToFind as parameters. The function will return the index of the first occurrence of the letter (case INsensitive) in the string searching from left to right. Return –1 if the letter is not in the string. Do NOT change the source string.

Write a subroutine that takes strDestination and strSource as parameters. The subroutine will append the source string to the end of the destination string. Assume the destination string is large enough to hold all the characters.

Write a subroutine that takes strDestination and strSource as parameters. The subroutine will copy the source string to the destination string in reverse order. Do NOT change the source string.

Write a subroutine that takes strDestination and strSource as parameters. The subroutine will copy the source string to the destination string and make the destination string all uppercase. Do NOT change the source string.

Write a subroutine that takes strDestination, strSource, intStartIndex and intLength as parameters. The subroutine will copy a substring from the source string to the destination string starting at intStartIndex and of length intLength. Do NOT change the source string.

Write a function that takes strSource as a parameter. The function will return the number of words in the string. For example the sentence “Mary had a little lamb.” has 5 words. Counting words is not the same thing as counting spaces. Do NOT change the source string.

Call each of the above functions from your main subroutine and display the results. For example:

void main( )

{

            char strSource[ 50 ] = "I Love Star Trek";

            int intLength = 0;

            char strDestination[ 50 ] = "";

            // Problem #1: String length

            intLength = StringLength( "I Love Star Trek" );

            printf( "Problem #1: String Length: %d\n", intLength );

            printf( "\n" );

            // Problem #2: CopyString

            CopyString( strDestination, "I Love Star Trek" );

            printf( "Problem #2: CopyString: %s\n", strDestination );

            printf( "\n" );

            etc

}

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

Code:

#include<stdio.h>
#include<malloc.h>

//Problem #1
int length_of_string(char *strSource)
{
    int i, len = 0;
    for(i = 0; strSource[i] != '\0'; i++)//iterate until find end of string \0
    {
        len++;//increment the length.
    }
    return len;
}

//Problem #2
void copy_string(char *strSource, char *strDestination)
{
    int i;
    for(i=0; strSource[i] != '\0'; i++ )//iterate until find end of string \0
    {
        strDestination[i] = strSource[i];//Copy
    }
    strDestination[i] = '\0';//Add \0 to indicate end of string.
    printf("\nProblem #2: After copy the destination string = %s", strDestination);
}

//Problem #3
int letter_to_find(char *strSource, char chrLetterToFind)
{
    int i;
    //If chrLetterToFind is uppercase, make it lowercase.
    if(chrLetterToFind >= 65 && chrLetterToFind <= 90)
        chrLetterToFind = chrLetterToFind + 32;
    for(i=0; strSource[i] != '\0'; i++)
    {
        //If character in the source string is lowercase
        if(strSource[i] >=97 && strSource[i] <= 122)
        {
            if(strSource[i] == chrLetterToFind)//Check for the match.
                return i;//Return the index.
        }
        else{
            if(strSource[i] + 32 == chrLetterToFind)//If the character in the source string is uppercase make
                //it lowercase and then check for the match.
                return i;
        }
    }
    return -1;
}

//Problem #4
void append_source(char *strSource, char *strDestination)
{
    int i , j;
    i = length_of_string(strDestination);//length of the string.
    for(j = 0; strSource[j] != '\0'; j++)
    {

        strDestination[i] = strSource[j];//add characters at the end.

        i++;
    }
    strDestination[i]= '\0';
    printf("\nProblem #4: After appending the content of destination string: %s", strDestination);

}

//Problem #5
void copy_reverse_order(char *strSource, char *strDestination)
{
    int i = length_of_string(strSource);
    int j, k;
    for(j = i-1, k = 0; j != 0; j--, k++)//Iterate from the last of the source.
    {
        strDestination[k] = strSource[j];
    }
    strDestination[k] = strSource[0];
    strDestination[++k] = '\0';
    printf("\nProblem #5: Copy in reverse order: %s", strDestination);
}

//Problem #6
void uppercase_string(char *strSource, char *strDestination)
{
    int i;
    for(i = 0; strSource[i] != '\0'; i++)
    {
        if(strSource[i] >= 97 && strSource[i]<= 122)//If lowercase
        {
            strDestination[i] = strSource[i] - 32;//Convert to uppercase.
        }

        else{//Do nothing just copy.
            strDestination[i] = strSource[i];
        }
    }
    strDestination[i] = '\0';
    printf("\nProblem #6: String in Uppercase: %s", strDestination);
}

//Problem #7
void copy_upto_length(char *strSource, char *strDestination, int StartIndex, int Length)
{
    int i, j;
    for(j = 0, i = StartIndex; i != Length; i++, j++)
    {
        strDestination[j] = strSource[i];
    }
    strDestination[j] = strSource[i];
    strDestination[++j] = '\0';
    printf("\nProblem #7: Copy upto the length: %s", strDestination);
}

//Problem #8
int number_of_words(char *strSource)
{
    int i, word = 0;
    for(i = 0; strSource[i] != '\0'; i++)
    {
        if(strSource[i] == 32 || strSource[i] == '.')
            word++;
    }
    return word;
}
int main()
{
    char *myString = "Mary had a little lamb.";
    char *str1 = (char *)malloc(sizeof(myString));//Allocate memory.
    int l = length_of_string(myString);
    printf("\nProblem #1: String Length = %d", l);

    copy_string(myString, str1);

    int index = letter_to_find(myString, 'm');
    printf("\nProblem #3:Index is = %d", index);

    char str2[50] = "This is another string";
    append_source(myString, str2);

    char str5[50];
    copy_reverse_order(myString, str5);

    char str3[20];
    copy_upto_length(myString, str3, 5, 10);

    char str4[20];
    uppercase_string(myString, str4);

    int word = number_of_words(myString);
    printf("\nProblem #8: Number of words = %d", word);
    return 0;
}

Output:

Problem #1: String Length 23 Problem #2 :. After copy the destination string -Mary had a little ianb. Problem #3: Index is Pr

Add a comment
Know the answer?
Add Answer to:
I need this in C not C++ just C SET-151 C Programming #1 Homework: 7 String...
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
  • Please note that I cannot use the string library function and that it must be coded...

    Please note that I cannot use the string library function and that it must be coded in C89, thank you! Formatting: Make sure that you follow the precise recommendations for the output content and formatting: for example, do not change the text in the first problem from “Please enter a string of maximum 30 characters:” to “Enter string: ”. Your assignment will be auto-graded and any changes in formatting will result in a loss in the grade. 2.Comments: Header comments...

  • CSC Hw Problems. Any help is appreciated I dont know where to start let alone what...

    CSC Hw Problems. Any help is appreciated I dont know where to start let alone what the answers are. Your assignment is to write your own version of some of the functions in the built-in <string.h> C library. As you write these functions, keep in mind that a string in C is represented as a char array, with the '\0' character at the end of the string. Therefore, when a string is passed as a parameter, the length of the...

  • Python 3.6 I JUST NEED HELP WITH PROBLEM 2 (CONTINUATION OF PROBLEM 1)!!! THIS IS MY...

    Python 3.6 I JUST NEED HELP WITH PROBLEM 2 (CONTINUATION OF PROBLEM 1)!!! THIS IS MY WORK FOR PROBLEM 1 (I NEED HELP WITH PROBLEM 2): Python 3.6 Problem 1 This problem provides practice using a while Irue loop Write a function named twoWords that gets and returns two words from a user. The first word is of a specified length, and the second word begins with a specified letter. The function twoWords takes two parameters: 1. an integer, length,...

  • Note that the main function that I have provided does use <string.h> as it constructs test...

    Note that the main function that I have provided does use <string.h> as it constructs test strings to pass to your functions. However, your solutions for the 5 functions below may not use any of the built-in C string functions from the <string.h> library. Write a function called strcmp373. This function is passed two parameters, both of which are C strings. You should use array syntax when writing this function; that is, you may use [ ], but not *...

  • In Programming language C - How would I convert my words char array into a string...

    In Programming language C - How would I convert my words char array into a string array so I can use the strcmp() function and alphabetically sort the words? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]){ int i =0; int j =0; int count =0; int length = strlen(argv[1]); for(i =0; i < length; i++){ if(isalpha(argv[1][i]) == 0 ||isdigit(argv[1][i] != 0)){ count ++; } printf("%c",argv[1][i]); } char *strings; int wordNum =0; int charNum =0; strings...

  • In C Programming Language In this lab you will implement 4 string functions, two using array...

    In C Programming Language In this lab you will implement 4 string functions, two using array notation and two using pointers. The functions must have the signatures given below. You may not use any C library string functions. The functions are 1. int my strlen (char s ) - This function returns the number of characters in a string. You should use array notation for this function. 2. int my strcpy (char s [], char t I)- This function overwrites...

  • Hey everyone, I need help making a function with this directions with C++ Language. Can you...

    Hey everyone, I need help making a function with this directions with C++ Language. Can you guys use code like printf and fscanf without iostream or fstream because i havent study that yet. Thanks. Directions: Write a function declaration and definition for the char* function allocCat. This function should take in as a parameter a const Words pointer (Words is a defined struct) The function should allocate exactly enough memory for the concatenation of all of the strings in the...

  • C++ Write a recursive function that reverses the given input string. No loops allowed, only use...

    C++ Write a recursive function that reverses the given input string. No loops allowed, only use recursive functions. Do not add more or change the parameters to the original function. Do not change the main program. #include <iostream> #include <string> using namespace std; //Write a recursive function 'void reverse(string &str)' that reverses the given input string. void reverse(string &str) { /*Code needed*/ } int main() {    string name = "sherry";    reverse(name);    cout << name << endl; //should...

  • USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is...

    USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is one of the simplest encryption techniques in the field of cryptography. It is a cipher in which each letter in a plain text message is replaced by a letter some fixed number of positions up the alphabet (i.e., by right shifting the alphabetic characters in the plain text message). For example, with a right shift of 2, ’A’ is replaced by ’C’, ’B’ is...

  • LOVOU AWN 1. def remove_all_from_string(word, letter): i=0 3. while i<len(word)-1: if word[i] = letter: num =...

    LOVOU AWN 1. def remove_all_from_string(word, letter): i=0 3. while i<len(word)-1: if word[i] = letter: num = word.find(letter) word - word[:num] + word[num+1:] i=0 i=i+1 return word 10 print remove_all_from_string("hello", "1") 5 points Status: Not Submitted Write a function called remove_all_from_string that takes two strings, and returns a copy of the first string with all instances of the second string removed. This time, the second string may be any length, including 0. Test your function on the strings "bananas" and "na"....

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