C++ Read String With Spaces From File
In C programming, a cord is a sequence of characters terminated with a zilch character \0. For example:
char c[] = "c cord"; When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a null character \0 at the end past default.
How to declare a string?
Hither's how y'all can declare strings:
char south[v];
Hither, we have declared a string of 5 characters.
How to initialize strings?
Y'all can initialize strings in a number of means.
char c[] = "abcd"; char c[50] = "abcd"; char c[] = {'a', 'b', 'c', 'd', '\0'}; char c[5] = {'a', 'b', 'c', 'd', '\0'};
Let'southward take another example:
char c[5] = "abcde"; Here, we are trying to assign half-dozen characters (the concluding character is '\0') to a char assortment having five characters. This is bad and yous should never do this.
Assigning Values to Strings
Arrays and strings are 2nd-class citizens in C; they do not back up the assignment operator once it is declared. For example,
char c[100]; c = "C programming"; // Error! assortment blazon is not assignable. Note: Apply the strcpy() function to copy the string instead.
Read String from the user
You can use the scanf() function to read a string.
The scanf() function reads the sequence of characters until information technology encounters whitespace (space, newline, tab, etc.).
Example 1: scanf() to read a cord
#include <stdio.h> int main() { char name[20]; printf("Enter proper name: "); scanf("%south", name); printf("Your name is %s.", name); return 0; } Output
Enter name: Dennis Ritchie Your name is Dennis.
Even though Dennis Ritchie was entered in the above program, simply "Dennis" was stored in the name cord. Information technology's because in that location was a space after Dennis.
Also discover that we accept used the lawmaking name instead of &proper name with scanf().
scanf("%s", name); This is considering name is a char array, and we know that assortment names decay to pointers in C.
Thus, theproper noun inscanf() already points to the address of the offset element in the string, which is why nosotros don't need to use &.
How to read a line of text?
You tin can use the fgets() function to read a line of string. And, you can use puts() to display the string.
Case 2: fgets() and puts()
#include <stdio.h> int master() { char name[30]; printf("Enter proper name: "); fgets(name, sizeof(proper noun), stdin); // read string printf("Name: "); puts(name); // display string render 0; } Output
Enter name: Tom Hanks Proper name: Tom Hanks
Here, we accept used fgets() function to read a string from the user.
fgets(proper name, sizeof(name), stdlin); // read string
The sizeof(proper noun) results to thirty. Hence, we can accept a maximum of 30 characters as input which is the size of theproper name string.
To print the cord, nosotros accept used puts(proper name);.
Note: The gets() office tin also be to take input from the user. However, it is removed from the C standard.
It'due south because gets() allows yous to input any length of characters. Hence, there might be a buffer overflow.
Passing Strings to Functions
Strings tin be passed to a part in a similar mode equally arrays. Learn more about passing arrays to a function.
Example 3: Passing cord to a Office
#include <stdio.h> void displayString(char str[]); int main() { char str[fifty]; printf("Enter string: "); fgets(str, sizeof(str), stdin); displayString(str); // Passing string to a function. render 0; } void displayString(char str[]) { printf("String Output: "); puts(str); } Strings and Pointers
Like similar arrays, string names are "decayed" to pointers. Hence, you tin can use pointers to manipulate elements of the string. We recommended you to check C Arrays and Pointers before you lot check this case.
Example 4: Strings and Pointers
#include <stdio.h> int main(void) { char name[] = "Harry Potter"; printf("%c", *name); // Output: H printf("%c", *(name+i)); // Output: a printf("%c", *(proper name+7)); // Output: o char *namePtr; namePtr = name; printf("%c", *namePtr); // Output: H printf("%c", *(namePtr+1)); // Output: a printf("%c", *(namePtr+seven)); // Output: o } Unremarkably Used String Functions
- strlen() - calculates the length of a string
- strcpy() - copies a string to another
- strcmp() - compares two strings
- strcat() - concatenates two strings
Source: https://www.programiz.com/c-programming/c-strings
0 Response to "C++ Read String With Spaces From File"
Postar um comentário