I had an assignment to write a program that takes 2 arguments as string by getchar
and saves in variables. The 2nd string defines the length of the 1st string, which means that if the first string has 23 characters and the 2nd one has 13, the code will print the 13 first characters of the 1st string and will remove the rest . the question contained a code and I had to complete the missing parts. I wrote my code but my program gives me the output in a loop and without the for
loop it doesn't work properly.
I couldn't understand why didn't it work so I would be very thankful and happy if anyone could help me.
input:
string 1: this is a sample string
(23 chars)
string 2: sample length
(13 chars)
output :
this is a sam
(13 chars)
the original code was this:
/*Gets one line of data from standard input. Returns an empty string on
end of file. If data line will not fit in allotted space, stores
portion that does fit and discards rest of input line.*/
char *
scanline(char *dest, /* output - destination string */
int dest_len) /* input - space available in dest */
{
int i, ch;
/* Gets next line one character at a time. */
i = 0;
for (ch = getchar();
ch != '\n' && ch != EOF && i < dest_len - 1;
ch = getchar())
dest[i++] = ch;
dest[i] = '\0';
/* Discards any characters that remain on input line */
while (ch != '\n' && ch != EOF)
ch = getchar();
return (dest);
}
The following code is mine which has problem with the output and might have other bugs but it was my best try:
#include <stdio.h>
#include <string.h>
#define BuffSize 256
char *scanline(char *dest, int dest_len)/* output - destination string */ /* input - space available in dest */
{
int i, ch;
char temp[BuffSize];
//get the first string and store in temp
i = 0;
for (ch = getchar(); ch != '\n' && ch != EOF && i < BuffSize; ch = getchar())
{temp[i++] = ch;
temp[i] = '\0';}
int len= strlen(temp);
printf("enter second string\n");
/* Gets next line one character at a time. */
i = 0;
int cha;
for (cha = getchar(); cha != '\n' && cha != EOF && i < dest_len - 1; cha = getchar())
{dest[i++] = cha;
dest[i] = '\0';}
dest_len= strlen(dest);
strncpy(dest, temp, dest_len);
dest[dest_len]='\0';
/* Discards any characters that remain on input line */
while (ch != '\n' && ch != EOF)
ch = getchar();
return (dest);
}
int main()
{
char dest[BuffSize];
char temp[BuffSize];
int dest_len;
printf("enter string\n");
scanline(dest, dest_len);
//my code has problem without the following loop
for (int i=0; i<=dest_len; i++)
{
printf("%s", dest);
}
return 0;
}
my problem in code was this part
for (int i=0; i<=dest_len; i++)
{
printf("%s", dest);
}
so basically I changed dest_len
with sizeof(dest)
as sizeof()
returns the size required here by the pointer type, so the code worked properly without referring to dest_len
which was not initialized. This is the final code that didn't have bug and gave the correct output:
#include <stdio.h>
#include <string.h>
#define BuffSize 256
char *scanline(char *dest, int dest_len)/* output - destination string */ /* input - space available in dest */
{
int i, ch;
char temp[BuffSize];
//get the first string and store in temp
i = 0;
for (ch = getchar(); ch != '\n' && ch != EOF && i < BuffSize; ch = getchar())
{temp[i++] = ch;
temp[i] = '\0';}
int len= strlen(temp);
printf("enter second string\n");
/* Gets next line one character at a time. */
i = 0;
int cha;
for (cha = getchar(); cha != '\n' && cha != EOF && i < dest_len - 1; cha = getchar())
{dest[i++] = cha;
dest[i] = '\0';}
dest_len= strlen(dest);
strncpy(dest, temp, dest_len);
dest[dest_len]='\0';
/* Discards any characters that remain on input line */
while (ch != '\n' && ch != EOF)
ch = getchar();
return (dest);
}
int main()
{
char dest[BuffSize];
printf("enter string\n");
printf("%s", scanline(dest, sizeof(dest)));
return 0;
}
output:
enter string
this is a sample line
enter second string
sample length
this is a sam
Process returned 0 (0x0) execution time: 11.890 s
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.