Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Tip1: How read id ,name and address from text file. char filename[100]=\"IDRand5

ID: 3910561 • Letter: T

Question

Tip1: How read id ,name and address from text file.

char filename[100]="IDRand500.txt";

FILE *fp=fopen(filename,"r");

char id[20],name[20],address[50];

while(!feof(fp))

{

fscanf(fp,"%s %s %s",id,name,address);

}

Tip2: convert string to number, "1998" => 1998

char s1[10]="1998"; int year;

year=atoi(s1)

Tip3: how to get substring

//from the source string(src),starting at start, get a substring of len lenght. The substring save in the dst, for example

char s1[30]="abcdefghimnlo", s2[10];

substr(s1,0,3,s2); //3 starting from the beginning, copy 3 character to s2;"abc"

printf("%s ",s2);

substr(s1,3,5,s2); //3 starting from the beginning, copy 3 character to s2;"defgh"

printf("%s ",s2);

int substr(char src[], int start, int len, char dst[])

{ if(dst==NULL || src==NULL)

return 0;

int ret = 0;

while (start)

{ src++;

start--;

}

if (strlen(src) < len)

{ len = strlen(src);

}

ret = len;

while (len)

{

*dst++ = *src++; len--;

}

*dst = '';

return ret;

}

Explanation / Answer

Tip1: PROGRAM

#include<stdio.h>

// File Content is following

/*  IDRand500.txt"

A1002 xxxx milon-street,flat:102

B1033 yyyy dream-lines-street,flat:494

C3945 ZZZZ washingtonDC,flat-333 */

int main()

{

FILE *fp; // Declare file pointer

// declare array of character "filename"

// with constant file using filepath (f:\IDRand500.txt")

char filename[100]="f:\IDRand500.txt";

char id[20],name[20],address[50]; // declare character of array variables

fp=fopen(filename,"r"); // open file in read mode

// display heading

printf(" ID NAME ADDRESS ");

printf("---------------------- ");

while(!feof(fp)) // create while loop until end of file

{

fscanf(fp,"%s %s %s",id,name,address); // reading each value from the file

printf("%s %s %s ",id,name,address); // display in tabular format

}

return 0;

}

OUTPUT


ID NAME ADDRESS
----------------------
A1002 xxxx milon-street,flat:102
B1033 yyyy dream-lines-street,flat:494
C3945 ZZZZ washingtonDC,flat-333

Tip2: PROGRAM

#include<stdio.h>

#include<stdlib.h> // atoi() function

int main()

{

char s1[10]="1998"; // declare constant string "1998"

int year; // declare integer variable year

year=atoi(s1); // calling atoi() function convert string to integer

printf(" Year: %d",year); // print actual year in integer

return 0;

}

OUTPUT

Year: 1998

Tip3: PROGRAM

#include<stdio.h>

#include<string.h> // using strlen() function

// declare prototype of substr() function

int substr(char src[],int start,int len,char dst[]);

int main()

{

// declare array of chacter constant s1 and s2

char s1[30]="abcdefghimnlo",s2[10];

printf(" Original String is: %s ",s1); // display Original string in s1

substr(s1,0,3,s2); // calling substr() function

printf("Substring from 0 position to 3 characters is: %s ",s2); // display resultant substring s2

substr(s1,3,5,s2); // calling substr() function

printf("Sinstring from 3 position to 5 characters is: %s ",s2); // display resultant substring s2

return 0;

}

// implement substr() function with four parameters

int substr(char src[],int start,int len,char dst[])

{

int ret=0; // initialize integer variable ret=0

// check condition whether src and dst of formal parameter in the function is NULL

// then, return 0

if(dst==NULL||src==NULL) return 0;

while(start) // create while loop until starting position of string

{

src++; // increment src characters, finally stores substring in src

start--; // decrement starting position of chacters

}

if(strlen(src)<len) // check condition whether length of src<len

len=strlen(src); // then, store total chacters in len

ret=len; // assign length of substring character in ret

while(len) // create while until length of substring

{

*dst++=*src++; // assign each character into dst(destination string)

len--; // decrement len

}

*dst=''; // assign destination string null character

return ret; // return ret value

}

OUTPUT


Original String is: abcdefghimnlo
Substring from 0 position to 3 characters is: abc
Sinstring from 3 position to 5 characters is: defgh

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote