Programming in C Problem 1 In this program you will write a subset of the string
ID: 3814247 • Letter: P
Question
Programming in C
Problem 1
In this program you will write a subset of the string manipulation functions found in the string
standard library. The only functions you can use (in the implementation of these functions) are
putc() and getc() (plus any functions that you write). Place these functions in the file
my_string.c and place the prototypes in my_string.h. Your main should include my_string.h and
demonstrate the use of each of the following functions (which should behave the same as the
corresponding function that is not prefixed with “my_”).
my_strlen()
my_strcpy()
my_strcat()
my_strncpy()
my_strncat()
Problem 2
Instead of writing my_printf(), you will write output functions that perform specific, sometimes
simplified, basic capabilities of printf(). All functions output is to the standard output. All
functions return the number of characters printed. Place these in my_string.c. Have your main.c
test each of these for several different cases – try to break them!
int printC(int c); // Outputs the character c if it is printable
int printS(const char *s); // Outputs the string s
int printI(int i); // Outputs the integer i as a base-10 string
int printX(int i); // Outputs the integer i as a hexadecimal string
int printF(double x, int n); // Format: fff.ffff
Outputs x as a fixed point string with n digits to the right of the decimal point. Value should be
properly rounded. If n < 0, do not print the decimal point (otherwise the same as n = 0).
int printE(double x, int n); // Format: f.fffea (x = f.fff * 10^a)
Outputs x in exponential notation with exactly one non-zero digit to the left of the decimal
point (unless x = 0) and n digits to the right, a lowercase ‘e’, and the exponent. If n < 0, do not
print the decimal point (otherwise same as n = 0).
Explanation / Answer
HI, I have implemented Q1.
Please repost other question in separate post
Please let me know in case of any issue in Q1
Please let me know in case of any issue.
############### mystring.h ##########
int my_strlen(char * str);
char * mystrcpy(char * des,char * src);
char *my_strncat(char *dest, char *src, int nb);
char *my_strcat(char *dest, char *src);
char *my_strncpy(char *dest, char *src, int n);
########### my_string.c ###############
#include <stdio.h>
#include "my_string.h"
int my_strlen(char * str)
{
int i = 0;
while (str[i] != '')
i++;
return i;
}
char * mystrcpy(char * des,char * src){
char * temp = des;
while(*src){
*temp = *src;
temp++;
src++;
}
return des;
}
char *my_strcat(char *dest, char *src)
{
int len;
int i;
len = my_strlen(dest);
i = 0;
while(src[i])
{
dest[len + i] = src[i];
i = i + 1;
}
dest[len + i] = '';
return (dest);
}
char *my_strncpy(char *dest, char *src, int n)
{
int i;
int max_str;
i = 0;
max_str = my_strlen(src);
while ( i < n)
{
dest[i] = src[i];
i = i + 1;
if (n >= max_str)
dest[max_str + 1] = '';
}
return (dest);
}
char *my_strncat(char *dest, char *src, int nb)
{
int dest_length;
int i;
dest_length = my_strlen(dest);
i = 0;
while (src[i] != '' && i < nb)
{
dest[dest_length + i] = src[i];
i = i + 1;
}
return (dest);
}
int main(){
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.