Chapter 08 Program Exercises 4) PE 08 04 (Comma Delimited Data) Create a single
ID: 3855967 • Letter: C
Question
Chapter 08 Program Exercises 4) PE 08 04 (Comma Delimited Data) Create a single string literal with data s by commas (create your own data). For example, "COP2270 C for Engineers,Miami Dade College,7117 Wolfson Campus,300 NE 2nd Ave,Miami FL 33132". Write a program using the strtok() function to read the comma delimited string and output the data in a format similar to: COP2270 c for Engineers Miami Dade College 7118 Wolfson Campus 300 NE 2nd AVE Miami FL 33132 5) PE 08 05 (Copy n String Characters) Write a program that uses a predefined string literal and an empty string. Prompt the user to enter the total number n of characters to copy. Copy only the first n characters of the string literal into the empty string using only the functions discussed in Chapter 8. Although, this program can be written with a loop, use only one or more of the functions discussed. Display the original string literal and the copied result to the screen for comparison.Explanation / Answer
4.The C library function char *strtok(char *str, const char *delim) breaks string str into a series of tokens using the delimiter delim and returns a pointer to the last token found in the string. A null pointer is returned if there are no tokens left to retrieve.For your question the delim is ",".And the code is:
#include<stdio.h>
#include<string.h> //to use the strtok function we have to include string.h
int main()
{
char str[1000]; //A string of max 1000 characters
printf("Enter the string:");
scanf("%s",str); //scanning the string into str
char *ptr;
ptr=strtok(str,","); // pointer to the first token found in the string
while(ptr!=NULL) //A null pointer is returned if there are no tokens left to retrieve.
{
printf("%s ",ptr);
ptr=strtok(NULL,","); //pointer to the next token
}
return 0;
}
5.The strncpy function copies the initial count characters of strSource to strDest and returns strDest. If count is less than or equal to the length of strSource, a null character is not appended automatically to the copied string. If count is greater than the length of strSource, the destination string is padded with null characters up to length count. The behavior of strncpy is undefined if the source and destination strings overlap. The code for your question is:
#include<stdio.h>
#include<string.h> //To use string methods we have to include string.h
int main()
{
char str[1000]; //A string of max 1000 characters
int n;
printf("Enter the original string:");
gets(str); //scans the string into str with spaces
printf("Enter how many characters to copy:");
scanf("%d",&n);
char cpystr[n];
strncpy(cpystr,str,n); //Copies first n characters of str to cpystr
printf(" Original string is:%s",str);
printf(" Copied string is:%s",cpystr);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.