Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 please dont use argc or argv use strings not char thanks!

MORE INFO

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

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

Database Systems For Advanced Applications 9th International Conference Dasfaa 2004 Jeju Island Korea March 2004 Proceedings Lncs 2973

Authors: YoonJoon Lee ,Jianzhong Li ,Kyu-Young Whang

2004th Edition

3540210474, 978-3540210474

More Books

Students also viewed these Databases questions