sSTRINGS reading a line as a String The function char *gets(char *str) reads a l
ID: 3704240 • Letter: S
Question
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 ] = ''; // 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 '' .
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!
Explanation / Answer
Please let me know if you have any doubt.
#include <stdio.h>
#include <string.h>
int main()
{
char Street_Address[100];
char Address_Line[100];
char City[100];
char State[100];
char Zip_Code[15];
printf(" Enter the Street Address: ");
gets(Street_Address);
printf(" Enter the Address Line: ");
gets(Address_Line);
printf(" Enter the City: ");
gets(City);
printf(" Enter the State: ");
gets(State);
printf(" Enter the Zip Code: ");
gets(Zip_Code);
strcat(Street_Address," ");
strcat(Street_Address,Address_Line);
strcat(Street_Address," ");
strcat(Street_Address,City);
strcat(Street_Address,", ");
strcat(Street_Address,State);
strcat(Street_Address," ");
strcat(Street_Address,Zip_Code);
printf(" Complete Addres: %s ",Street_Address);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.