NEW 0x06. C - More Pointers, Arrays and Strings
NEW 0x06. C - More Pointers, Arrays and Strings
md
#TASKS
0. strcat
1. strncat
2. strncpy
3. strcmp
Write a function that compares two strings.
5. Always look up
6. Expect the best. Prepare for the worst. Capitalize on what comes
7. Mozart composed his music not for the elite, but for everybody
8. rot13
Add one line to this code, so that the program prints a[2] = 98, followed
by a new line.
You are not allowed to use the variable a in your new line of code
You are not allowed to modify the variable p
You can only write one statement
You are not allowed to use ,
You are not allowed to code anything else than the line of expected
line of code at the expected line
Your code should be written at line 19, before the ;
Do not remove anything from the initial code (not even the comments)
and don’t change anything but the line of code you are adding
(don’t change the spaces to tabs!)
You are allowed to use the standard library
Prototype: char *infinite_add(char *n1, char *n2, char *r, int size_r);
Where n1 and n2 are the two numbers
r is the buffer that the function will use to store the result
size_r is the buffer size
The function returns a pointer to the result
You can assume that you will always get positive numbers, or 0
You can assume that there will be only digits in the strings n1 and
n2
n1 and n2 will never be empty
If the result can not be stored in r the function must return 0
#ifndef MAIN_H
#define MAIN_H
#endif
_putchar.c
#include "main.h"
#include <unistd.h>
/**
* _putchar - writes the character c to stdout
* @c: The character to print
*
* Return: On success 1.
* On error, -1 is returned, and errno is set appropriately.
*/
int _putchar(char c)
{
return (write(1, &c, 1));
}
0-strcat.c
#include "main.h"
/**
* _strcat - concatenates two strings
* @dest: input value
* @src: input value
*
* Return: void
*/
char *_strcat(char *dest, char *src)
{
int i;
int j;
i = 0;
while (dest[i] != '\0')
{
i++;
}
j = 0;
while (src[j] != '\0')
{
dest[i] = src[j];
i++;
j++;
}
dest[i] = '\0';
return (dest);
}
1-strncat.c
#include "main.h"
/**
* _strncat - concatenate two strings
* using at most n bytes from src
* @dest: input value
* @src: input value
* @n: input value
*
* Return: dest
*/
char *_strncat(char *dest, char *src, int n)
{
int i;
int j;
i = 0;
while (dest[i] != '\0')
{
i++;
}
j = 0;
while (j < n && src[j] != '\0')
{
dest[i] = src[j];
i++;
j++;
}
dest[i] = '\0';
return (dest);
}
2-strncpy.c
#include "main.h"
/**
* _strncpy - copy a string
* @dest: input value
* @src: input value
* @n: input value
*
* Return: dest
*/
char *_strncpy(char *dest, char *src, int n)
{
int j;
j = 0;
while (j < n && src[j] != '\0')
{
dest[j] = src[j];
j++;
}
while (j < n)
{
dest[j] = '\0';
j++;
}
return (dest);
}
3-strcmp.c
#include "main.h"
/**
* _strcmp - compare string values
* @s1: input value
* @s2: input value
*
* Return: s1[i] - s2[i]
*/
int _strcmp(char *s1, char *s2)
{
int i;
i = 0;
while (s1[i] != '\0' && s2[i] != '\0')
{
if (s1[i] != s2[i])
{
return (s1[i] - s2[i]);
}
i++;
}
return (0);
}
4-rev_array.c
#include "main.h"
/**
* reverse_array - reverse array of integers
* @a: array
* @n: number of elements of array
*
* Return: void
*/
void reverse_array(int *a, int n)
{
int i;
int t;
#include "main.h"
/**
* string_toupper - change all lowercase to uppercase
* @n: pointer
*
* Return: n
*/
char *string_toupper(char *n)
{
int i;
i = 0;
while (n[i] != '\0')
{
if (n[i] >= 'a' && n[i] <= 'z')
n[i] = n[i] - 32;
i++;
}
return (n);
}
6-cap_string.c
#include "main.h"
/**
* cap_string - Capitalizes all words of a string.
* @str: The string to be capitalized.
*
* Return: A pointer to the changed string.
*/
char *cap_string(char *str)
{
int index = 0;
while (str[index])
{
while (!(str[index] >= 'a' && str[index] <= 'z'))
index++;
index++;
}
return (str);
}
7-leet.c
#include "main.h"
/**
* leet - encode into 1337speak
* @n: input value
* Return: n value
*/
char *leet(char *n)
{
int i, j;
char s1[] = "aAeEoOtTlL";
char s2[] = "4433007711";
#include "main.h"
#include <stdio.h>
/**
* rot13 - encoder rot13
* @s: pointer to string params
*
* Return: *s
*/
#include "main.h"
/**
* print_number - print numbers chars
* @n: integer params
* Return: 0
*/
void print_number(int n)
{
unsigned int n1;
n1 = n;
if (n < 0)
{
_putchar('-');
n1 = -n;
}
if (n1 / 10 != 0)
{
print_number(n1 / 10);
}
_putchar((n1 % 10) + '0');
}
102-magic.c
#include <stdio.h>
int main(void)
{
int n;
int a[5];
int *p;
a[2] = 1024;
p = &n;
/*
* write your line of code here...
* Remember:
* - you are not allowed to use a
* - you are not allowed to modify p
* - only one statement
* - you are not allowed to code anything else than this line of code
*/
*(p + 5) = 98;
/* ...so that this prints 98\n */
printf("a[2] = %d\n", a[2]);
return (0);
}
103-infinite_add.c
#include "main.h"
/**
* rev_string - reverse array
* @n: integer params
* Return: 0
*/
/**
* infinite_add - add 2 numbers together
* @n1: text representation of 1st number to add
* @n2: text representation of 2nd number to add
* @r: pointer to buffer
* @size_r: buffer size
* Return: pointer to calling function
*/
char *infinite_add(char *n1, char *n2, char *r, int size_r)
{
int overflow = 0, i = 0, j = 0, digits = 0;
int val1 = 0, val2 = 0, temp_tot = 0;
104-print_buffer.c
#include "main.h"
#include <stdio.h>
/**
* print_buffer - prints buffer
* @b: buffer
* @size: size
* Return: void
*/
o = 0;
if (size <= 0)
{
printf("\n");
return;
}
while (o < size)
{
j = size - o < 10 ? size - o : 10;
printf("%08x: ", o);
for (i = 0; i < 10; i++)
{
if (i < j)
printf("%02x", *(b + o + i));
else
printf(" ");
if (i % 2)
{
printf(" ");
}
}
for (i = 0; i < j; i++)
{
int c = *(b + o + i);