How to resolve the algorithm Repeat a string step by step in the C programming language
How to resolve the algorithm Repeat a string step by step in the C programming language
Table of Contents
Problem Statement
Take a string and repeat it some number of times.
Example: repeat("ha", 5) => "hahahahaha"
If there is a simpler/more efficient way to repeat a single “character” (i.e. creating a string filled with a certain character), you might want to show that as well (i.e. repeat-char("*", 5) => "*****").
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Repeat a string step by step in the C programming language
C Function string_repeat
The string_repeat function takes two arguments:
n: The number of times to repeat the given string.s: The string to be repeated.
It returns a pointer to a new allocated string consisting of the given string repeated n times.
Implementation Details:
- It first calculates the length of the input string (
slen). - Then, it allocates memory for the new string, which will be
ntimes the length of the input string plus one for the null-terminator. - Using a
forloop, it copies the input string to the allocated memoryntimes. - Finally, it adds the null-terminator and returns the pointer to the new string.
C Function string_repeat (Alternative Implementation)
This is an alternative implementation of the string_repeat function that uses a different approach to achieve the same result:
- It first allocates memory for the new string, similar to the previous implementation.
- It then initializes two pointers,
paandpb, to point to different ends of the allocated memory. papoints to the first character of the new string, whilepbpoints to the last character.- It copies the input string to the end of the allocated memory, starting from the end.
- Then, it uses a
whileloop to copy the characters from the end of the input string to the beginning of the new string, overwriting any existing characters. - Finally, it returns the pointer to the new string.
C Function char_repeat
The char_repeat function takes two arguments:
n: The number of times to repeat the given character.c: The character to be repeated.
It returns a pointer to a new allocated string consisting of the given character repeated n times.
Implementation Details:
- It allocates memory for the new string, which will be
ncharacters plus one for the null-terminator. - It uses
memsetto fill the allocated memory with the given character. - Finally, it adds the null-terminator and returns the pointer to the new string.
Source code in the c programming language
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * string_repeat( int n, const char * s ) {
size_t slen = strlen(s);
char * dest = malloc(n*slen+1);
int i; char * p;
for ( i=0, p = dest; i < n; ++i, p += slen ) {
memcpy(p, s, slen);
}
*p = '\0';
return dest;
}
int main() {
char * result = string_repeat(5, "ha");
puts(result);
free(result);
return 0;
}
...
char *string_repeat(const char *str, int n)
{
char *pa, *pb;
size_t slen = strlen(str);
char *dest = malloc(n*slen+1);
pa = dest + (n-1)*slen;
strcpy(pa, str);
pb = --pa + slen;
while (pa>=dest) *pa-- = *pb--;
return dest;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * char_repeat( int n, char c ) {
char * dest = malloc(n+1);
memset(dest, c, n);
dest[n] = '\0';
return dest;
}
int main() {
char * result = char_repeat(5, '*');
puts(result);
free(result);
return 0;
}
You may also check:How to resolve the algorithm Knapsack problem/Unbounded step by step in the C programming language
You may also check:How to resolve the algorithm Hofstadter Figure-Figure sequences step by step in the C programming language
You may also check:How to resolve the algorithm Four bit adder step by step in the C programming language
You may also check:How to resolve the algorithm Plot coordinate pairs step by step in the C programming language
You may also check:How to resolve the algorithm Formal power series step by step in the C programming language