Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

sSTRINGS reading a line as a String The function char *gets(char *str) reads a line from STDIN and stores it into the string pointed to

sSTRINGS

reading a line as a String

The function char *gets(char *str) reads a line from STDIN and stores it into the string pointed to by str. It stops when either the newline character is read or when the end-of-file is reached, whichever comes first.

char sentence[100]; printf("Enter a sentence: "); gets(sentence);

Note: You should NEVER use gets function in production code because it has a buffer overflow vulnerability.

A safer alternative is the char *fgets(char * str, int num, FILE * stream) function which reads characters from stream and stores them into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.

printf("Enter sentence: ");

char sentence[10];

fgets(sentence, sizeof(sentence), stdin);

sentence[strlen(sentence) - 1 ] = '\0'; // To remove the ' ' character from the end.

One undesirable side effect of fgets it stores the new line character ' ' if it reads one in the input. This means that you will have to manually overwrite the position of the newline character say with '\0' .

String.h Library

Length of a String

The function size_t strlen(const char *str) returns the number characters in a string not counting the terminating null.

Concatenate Strings

The function char * strcat ( char * destination, const char * source ) appends the source string to the end of the destination string.

char str1[30] = "Jan 12,";

char str2[] = " 1842";

strcat (str1, str2);

printf("%s ", str1);

The output of this code will be :

Jan 12, 1842

String Tokens

The function char * strtok ( char * source, const char * delimiters) breaks the parameter source into tokens by finding group of characters separated by any of the delimiting characters .

#include

#include

#define COMMA ",."

int main(int argc, char const *argv[]) {

char date[] = "Jan 12, 1842. May 13, 2018";

char *token = strtok(date, COMMA);

while(token != NULL) {

printf("%s ", token);

token = strtok(NULL, COMMA);

}

}

The output of this code will be :

Jan 12

1842

May 13

2018

NOW THE TASK IS TO:

Write a simple program in C that asks the user for:

Street Address (Max 100 characters)

Address Line 2 (Maximum 100 characters)

City (Maximum 100 characters)

State (Maximum 100 characters)

Zip Code: Maximum 15 characters.

Your program should then a concatenate all the address fields together into one string and print out that string. Use strcat in this task.dont use malloc,struct or alloc.use strings msotly

thanks!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Third International Conference Achids 2011 Daegu Korea April 2011 Proceedings Part 2 Lnai 6592

Authors: Ngoc Thanh Nguyen ,Chong-Gun Kim ,Adam Janiak

2011th Edition

3642200419, 978-3642200410

Students also viewed these Databases questions

Question

Why is stakeholder engagement important?

Answered: 1 week ago

Question

6. Identify seven types of hidden histories.

Answered: 1 week ago

Question

What is the relationship between humans and nature?

Answered: 1 week ago