String in C programming language
In this tutorial, we are learning string in c programming .
Below is the topic we covered in this tutorial
What is string
Declaration and initialisation of string
String input: read a string
String output : print / display a string
String library function
What is string in c
String in c:-
String are defined as an array of characters.
a string of characters is an a sequence of data of data char (ASCII code) store in a consecutive memory locations.
String is is terminated by null character '\0'.
Ex.
String STR = "insportsoftware";
A string constant is a return within a double quotes.
Declaration of string:-
A string is a simple array with char as a data type.
C language does not directly support string as a data type, Hence, to display a string in c you need to make use of character array.
Syntax;
Declaring a variable as a string
char String_name[ String_length] = "String";
Initializing a string:-
Strength and weakness lies in different ways
below is an example of declaring a string with the name as a steam and initialislze itwith Saurabh.
char Str [ ] = "Saurbh";
char Str [ ] = "Saurabh";
char Str [ ] = { 'S','a','u','r','a','b','h'};
char Str [20] = { 'S','a','u','r','a','b','h'};
#include<stdio.h>
int main()
{
// declare and initialize string
char str[] = "Saurabh";
// print string
printf("%s",str);
return 0;
}
We can see in the above program ,we do not need to print a string, character by character. The C language does not provide an inbuilt data type for strings but it has an access specifier “%s” which can be used to directly print and read strings.
String input : read a string
When writing interactive programs which ask the user for input, C provides the scanf(), gets(), and fgets() functions to find a line of text entered from the user.
When we use scanf() to read, we use the "%s" format specifier without using the "&" to access the variable address because an array name acts as a pointer. For example:
#include <stdio.h>
int main() {
char name[10];
int age;
printf("Enter your first name and age: \n");
scanf("%s %d", name, &age);
printf("You entered: %s %d",name,age);
}
we use the gets() function. Gets ignores the whitespaces. It stops reading when a newline is reached (the Enter key is pressed).For example:
#include <stdio.h>
int main() {
char full_name[25];
printf("Enter your full name: ");
gets(full_name);
printf("My full name is %s ",full_name);
return 0;
}
String output : print / display a string:-
Enter your full name: Dennis Ritchie
My full name is Dennis Ritchie
Another safer alternative to gets() is fgets() function which reads a specified number of characters. For example:
#include <stdio.h>
int main() {
char name[10];
printf("Enter your name plz: ");
fgets(name, 10, stdin);
printf("My name is %s ",name);
return 0;}
the string name,
the number of characters to read,
stdin means to read from the standard input which is the keyboard.
Example,
printf("%s", name);
String output is done with the fputs() and printf() functions.
fputs() function
The fputs() needs the name of the string and a pointer to where you want to display the text. We use stdout which refers to the standard output in order to print to the screen.For example:
#include <stdio.h>
int main()
{char town[40];
printf("Enter your town: ");
gets(town);
fputs(town, stdout);
return 0;}
puts function
The puts function prints the string on an output device and moves the cursor back to the first position. A puts function can be used in the following way,
#include <stdio.h>
int main() {
char name[15];
gets(name); //reads a string
puts(name); //displays a string
return 0;}
The syntax of this function is comparatively simple than other functions.
Recommand for you:
String Library in C