How to Read in a Char for a Function C++
In C programming, a string is a sequence of characters terminated with a nix character \0
. For example:
char c[] = "c string";
When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a null character \0
at the end by default.

How to declare a string?
Hither's how you tin can declare strings:
char s[v];

Here, we have declared a string of five characters.
How to initialize strings?
Yous can initialize strings in a number of ways.
char c[] = "abcd"; char c[50] = "abcd"; char c[] = {'a', 'b', 'c', 'd', '\0'}; char c[5] = {'a', 'b', 'c', 'd', '\0'};

Let's take another example:
char c[5] = "abcde";
Here, we are trying to assign half dozen characters (the terminal grapheme is '\0'
) to a char
array having five characters. This is bad and you should never practise this.
Assigning Values to Strings
Arrays and strings are 2nd-class citizens in C; they exercise not support the assignment operator once it is declared. For example,
char c[100]; c = "C programming"; // Mistake! array type is not assignable.
Note: Utilise 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 it encounters whitespace (space, newline, tab, etc.).
Example 1: scanf() to read a string
#include <stdio.h> int main() { char name[xx]; printf("Enter name: "); scanf("%s", proper noun); printf("Your proper noun is %due south.", name); render 0; }
Output
Enter name: Dennis Ritchie Your name is Dennis.
Even though Dennis Ritchie was entered in the above program, only "Dennis" was stored in the name string. It's because there was a infinite afterward Dennis.
Likewise notice that nosotros take used the code name instead of &proper noun
with scanf()
.
scanf("%s", proper name);
This is because name is a char
array, and we know that array names decay to pointers in C.
Thus, theproper noun inscanf()
already points to the accost of the get-go element in the cord, which is why we don't demand to utilize &
.
How to read a line of text?
You tin can use the fgets()
office to read a line of string. And, you can use puts()
to display the string.
Case ii: fgets() and puts()
#include <stdio.h> int chief() { char name[thirty]; printf("Enter name: "); fgets(name, sizeof(name), stdin); // read string printf("Name: "); puts(proper name); // display cord render 0; }
Output
Enter name: Tom Hanks Name: Tom Hanks
Here, we take used fgets()
role to read a string from the user.
fgets(name, sizeof(name), stdlin); // read string
The sizeof(proper noun)
results to xxx. Hence, we can have a maximum of 30 characters equally input which is the size of thename string.
To impress the string, nosotros have used puts(name);
.
Note: The gets()
function can too be to take input from the user. Withal, it is removed from the C standard.
It's considering gets()
allows you to input any length of characters. Hence, there might be a buffer overflow.
Passing Strings to Functions
Strings tin can exist passed to a office in a similar way equally arrays. Learn more almost passing arrays to a part.
Case iii: Passing string to a Part
#include <stdio.h> void displayString(char str[]); int principal() { char str[fifty]; printf("Enter cord: "); fgets(str, sizeof(str), stdin); displayString(str); // Passing cord to a function. render 0; } void displayString(char str[]) { printf("String Output: "); puts(str); }
Strings and Pointers
Like like arrays, cord names are "decayed" to pointers. Hence, you can utilise pointers to manipulate elements of the string. We recommended you to check C Arrays and Pointers earlier you check this case.
Example 4: Strings and Pointers
#include <stdio.h> int primary(void) { char name[] = "Harry Potter"; printf("%c", *name); // Output: H printf("%c", *(proper name+1)); // Output: a printf("%c", *(proper name+7)); // Output: o char *namePtr; namePtr = name; printf("%c", *namePtr); // Output: H printf("%c", *(namePtr+one)); // Output: a printf("%c", *(namePtr+seven)); // Output: o }
Commonly Used String Functions
- strlen() - calculates the length of a cord
- strcpy() - copies a string to another
- strcmp() - compares two strings
- strcat() - concatenates 2 strings
Source: https://www.programiz.com/c-programming/c-strings
Postar um comentário for "How to Read in a Char for a Function C++"