Strings in C: Usage and Examples
Table of Content:
We can store a group of integers in an integer array, similarly, a group of characters can be stored in a character array.
A string constant or a String is a one-dimensional array of characters terminated by a null ( ‘\0’ ). This null character indicates the end of the string. Strings are always enclosed by double quotes. Whereas, a character is enclosed by single quotes in C.
In C, a string can be referred either using a character pointer or as a character array.
The following declaration and initialization create a string consisting of the word "atnyla". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "atnyla"
char stng[7] = {'a', 't', 'n', 'y', 'l', 'a', '\0'};
you can write the above statement as follows ?
char stng[] = "atnyla";
you do not place the null character at the end of a string constant. The C compiler automatically places the '\0' at the end of the string when it initializes the array.
Example C String:
Difference between above declarations are, when we declare char as string[11], 11 bytes
of memory space is allocated for holding the string value.
char string[11] = {'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', '\0'};
(or)
char string[11] = "helloworld";
When we declare char as string[], memory space will be allocated as per the requirement
during execution of the program.
char string [] = "helloworld";
String in C Example
Program
#includeint main () { char string1[11] = {'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', '\0'}; char string2[11] = "helloworld"; char string3 [] = "helloworld"; printf("The string is : %s \n", string1 ); printf("The string is : %s \n", string2 ); printf("The string is : %s \n", string3 ); return 0; }
Output
The string is : helloworld The string is : helloworld The string is : helloworld Press any key to continue . . .
scanf() to read a string
This C program illustrates how to read string from terminal.
Program
#includeint main() { char name[20]; printf("Enter Your name: "); scanf("%s", name); printf("Your name is %s.", name); return 0; }
Output
Output 1: Enter Your name: atnyla Your name is atnyla. Output 2: Enter Your name: Rambo Azmi Your name is Rambo.
Here, program ignores Azmi because, scanf() function takes only a single string before the white space, i.e. Rambo.
Reading a line of text Using getchar()
This program reads a full line of text and store each character one by one.
Program
#includeint main() { char line[30], ch; int i = 0; printf("Enter name: "); while(ch != '\n') // terminates if user hit enter { ch = getchar(); line[i] = ch; i++; } line[i] = '\0'; // inserting null character at end printf("Line is: %s", line); return 0; }
Output
Enter name: Hello reader how are you? Line is: Hello reader how are you? Press any key to continue . . .
In the program above, using the function getchar(),
ch gets a single character from the user each time.
This process is repeated until the user enters return (enter key). Finally, the null character is inserted at the end to make it a string.
Reading a line of text Using standard library function
Program
To make life easier, there are predefined functions gets() and puts in C language to read and display string respectively.
#includeint main() { char line[30]; printf("Enter name: "); gets(line); //Function to read string from user. printf("Name: "); puts(line); //Function to display string. return 0; }
Output
Enter name: welcome to atnyla! Name: welcome to atnyla! Press any key to continue . . .
- Question 1: How do you declare a variable that will hold string values?
- Question 2: What is wrong with this statement? myName = "Nurujjaman";
- Question 3: How do you determine the length of a string value that was stored in a variable?
- Question 4: How do you convert strings to numbers in C?
- Question 5: What is the difference between memcpy() and memmove() functions in C?
- Question 6: What is the difference between strcpy() and strncpy() functions in C?
- Question 7: What is the difference between array and string in C?
Related Questions
- Assignment 1: String in C Example
- Assignment 2: scanf() to read a string
- Assignment 3: Reading a line of text Using getchar()
- Assignment 4: Reading a line of text Using standard library function
- Assignment 5: Write a program that demonstrates the variety of ways that a string can be printed.
- Assignment 6: Write a program that demonstrates the way the sprintf( ) and puts( ) library functions work with strings.
- Assignment 7: Write a program that demonstrates the use of %s conversion specifier in scanf( ).
- Assignment 8: Write a program that demonstrates the use of scanset in scanf().
- Assignment 9: Write a program that demonstrates the joining of two strings using library function strcat( ).
- Assignment 10: C Program to Find the Length of a String
- Assignment 11: Write a program that demonstrates the use of width and number of character specifier with %s conversion specifier in printf( ) when used with strings.
- Assignment 12: C Program to Find the Frequency of Characters in a String
- Assignment 13: C program to Reverse a Sentence Using Recursion
- Assignment 14: C Program Concatenate Two Strings Without Using strcat()
- Assignment 15: C Program to Copy String Without Using strcpy()
- Assignment 16: C Program to Remove all Characters in a String Except Alphabet
- Assignment 17: C Program to Sort Elements in Lexicographical Order (Dictionary Order)