String in C
String in C
What is a 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 a character array.
The general syntax for declaring a variable as a string is as follows,
The above example represents string variables with an array size of 15.
This means that the given character array is capable of holding 15
characters at most. The indexing of array begins from 0 hence it will store
characters from a 0-14 position. The C compiler automatically adds a
NULL character '\0' to the character array created.
Let's study the initialization of a string variable. Following example
demonstrates the initialization of a string variable,
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.
scanf() function:
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);
}
Output:
The problem with the scanf function is that it never reads an entire string.
It will halt the reading process as soon as whitespace, form feed, vertical
tab, newline or a carriage return occurs. Suppose we give input as
"Guru99 Tutorials" then the scanf function will never read an entire string
as a whitespace character occurs between the two names. The scanf
function will only read Guru99.
gets() function:
In order to read a string contains spaces, 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 filename[25];
printf("Enter your full name: ");
gets(full_name);
printf("My full name is %s ",full_name);
return 0;
}
Output:
fgets() function:
#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;
}
Output:
Example,
printf("%s", name);
String output is done with the fputs(), puts() 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;
}
Output:
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;
}
Function Purpose
#include <stdio.h>
#include <string.h>
int main ()
{
//string initialization
char string1[15]="Hello";
char string2[15]=" World!";
char string3[15];
int val;
//string comparison
val= strcmp(string1,string2);
if(val==0)
{
printf("Strings are equal\n");
}
else
{
printf("Strings are not equal\n");
}
//string concatenation
printf("Concatenated:%s",strcat(string1,string2));
//string length
printf("\nLengthstring1 :%d",strlen(string1));
printf("\nLengthstring2: %d",strlen(string2));
//string copy
printf("\nCopied:%s\n",strcpy(string3,string1));
//string1 is copied into string3
return 0;
}
Output:
Review Questions:
1. Define Array.
2. What do you meant by one dimensional array?
3. How to declare an array in C Program?
4. How to access the array elements?
5. Explain the initialization of two dimensional arrays.
6. Discuss multi dimensional array with example.
7. Write a C program to add two matrices.
8. Define static array.
9. What are the possible ways available in C to create dynamic arrays?
10. What are the functions we need to dynamic memory allocation?
11. State the difference of static and dynamic array.
12. What is String?
13. How to bring the string variable in C program?
14. How can you read a string from terminal?
15. What can we do to display a string on screen?
16. Explain various string handling functions with appropriate C program.