I'm working on a question for a course at my school. I can't seem to use scanf()
for some reason, and I've got to use gets()
.
The question is as follows:
Write a C function stringncpy()
that copies not more
than n characters (characters that follow a null character
are not copied) from the array pointed to by s2
to the
array pointed to by s1
.
If the array pointed to by s2
is
a string shorter than n characters, null characters
are appended to the copy in the array pointed to by
s1
, until n characters in all have been written.
The
stringncpy()
returns the value of s1
.
The function prototype:
char *stringncpy(char * s1, char * s2, int n);
In addition, write a C program to test the stringncpy
function. Your program should read the string and the
target n characters from the user and then call the
function with the user input.
In this program, you are not allowed to use any functions from the C standard String library
When I run the program after a successful build, I keep getting the following error.
Enter the string:
this is atest
Enter the number of characters:
stringncpy(): ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠this
The code for the implementation is as follows:
#include <stdio.h>
char *stringncpy(char *s1, char *s2, int n);
int main()
{
char sourceStr[40], targetStr[40], *target;
int length;
printf("Enter the string: \n");
//scanf("%s",sourceStr); //doesn't work for some reason
gets(sourceStr);
printf("Enter the number of characters: \n");
scanf("%d", &length);
target = stringncpy(targetStr, sourceStr, length);
printf("stringncpy(): %s\n", target);
return 0;
}
char *stringncpy(char *s1, char *s2, int n)
{
/* Copy source to target */
int i, j;
for (i = 0; i < n; i++)
{
if (s2[i] == '\0')
{
break;
}
else
{
s1[i] = s2[i];
}
}
//s1[i + 1] = '\0';
for (j = i + 1; j <= n; j++)
{
s1[j] = '\0';
}
return s1;
}
Can anyone please tell me what's wrong with this? Thank you.