Strings
The string can be defined as the one-dimensional array of characters terminated by a null ('\0').
The character array or the string is used to manipulate text such as word or sentences.
Each character in the array occupies one byte of memory, and the last character must always be 0.
The termination character ('\0') is important in a string since it is the only way to identify where the string ends.
When we define a string as char s[10], the character s[10] is implicitly initialized with the null in the memory.
There are two ways to declare a string in c language.
- By char array
- By string literal
Let's see the example of declaring string by char array in C language.
char ch[10]={'c', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm','e', '\0'};
C gets() and puts() Functions
The gets() and puts() are declared in the header file stdio.h. Both the functions are involved in the input/output operations of the strings.
C gets() Function
The gets() function enables the user to enter some characters followed by the enter key.
All the characters entered by the user get stored in a character array.
The null character is added to the array to make it a string.
The gets() allows the user to enter the space-separated strings. It returns the string entered by the user.
Example Program using gets()
#include<stdio.h>
void main ()
{
char s[30];
printf("Enter the string? ");
gets(s);
printf("You entered %s",s);
}
Output:
Enter the string?
Am learning C program
You entered Am learning C program
The gets() function is risky to use since it doesn't perform any array bound checking and keep reading
the characters until the new line (enter) is encountered. It suffers from buffer overflow,
which can be avoided by using fgets(). The fgets() makes sure that not more than the maximum limit
of characters are read. Consider the following example.
#include<stdio.h>
void main()
{
char str[23];
printf("Enter the string? ");
fgets(str, 20, stdin);
printf("%s", str);
}
Output:
Enter the string? C is Low Level Language
C is Low Level Language
C puts() function
The puts() function is very much similar to printf() function. The puts() function is used to
print the string on the console which is previously read by using gets() or scanf() function.
The puts() function returns an integer value representing the number of characters being printed on the console.
Since, it prints an additional newline character with the string, which moves the cursor to the new line on the console,
the integer value returned by puts() will always be equal to the number of characters present in the string plus 1.
Let's see an example to read a string using gets() and print it on the console using puts().
#include
#include
int main(){
char name[50];
printf("Enter your Counttry: ");
gets(name); //reads string from user
printf("Your Country is: ");
puts(name); //displays string
return 0;
}
Output:
Enter your Country: India
Your Country is: India
C String Functions
There are many important string functions defined in "string.h" library.
| No. |
Function |
Description |
| 1) |
strlen(string_name) |
returns the length of string name. |
| 2) |
strcpy(destination, source) |
copies the contents of source string to destination |
| 3) |
strcat(first_string, second_string) |
concats or joins first string with second string. The result of the string is stored in first string. |
| 4) |
strcmp(first_string, second_string) |
compares the first string with second string. If both strings are same, it returns 0. |
| 5) |
strrev(string) |
returns reverse string. |
| 6) |
strlwr(string) |
returns string characters in lowercase. |
| 7) |
strupr(string) |
returns string characters in uppercase. |
C String Length: strlen() function
The strlen() function returns the length of the given string. It doesn't count null character '\0'.
#include<stdio.h>
#include <string.h>
int main(){
char ch[20]={'R', 'e', 's', 'h', 'm', 'a', 'n', '\0'};
printf("Length of string is: %d",strlen(ch));
return 0;
}
Output:
Length of string is: 5
C Copy String: strcpy()
The strcpy(destination, source) function copies the source string in destination.
#include<stdio.h>
#include <string.h>
int main(){
char ch[20] = {'R', 'e', 's', 'h', 'm', 'a', 'n', '\0'};
char ch2[20];
strcpy(ch2,ch);
printf("Value of First string is: %s",ch1);
printf("Value of second string is: %s",ch2);
return 0;
}
Output:
Value of First string is:Reshman
Value of second string is: Reshman
C String Concatenation: strcat()
The strcat(first_string, second_string) function concatenates two strings and result is returned to first_string.
#include<stdio.h>
#include <string.h>
int main(){
char ch[10]={'h', 'e', 'l', 'l', 'o', '\0'};
char ch2[10]={'c', '\0'};
strcat(ch,ch2);
printf("Value of first string is: %s",ch);
return 0;
}
Output:
Value of first string is: helloc
C Compare String: strcmp()
The strcmp(first_string, second_string) function compares two string and returns 0 if both strings are equal.
Here, we are using gets() function which reads string from the console.
#include<stdio.h>
#include <string.h>
int main(){
char str1[20],str2[20];
printf("Enter 1st string: ");
gets(str1);//reads string from console
printf("Enter 2nd string: ");
gets(str2);
if(strcmp(str1,str2)==0)
printf("Strings are equal");
else
printf("Strings are not equal");
return 0;
}
Output:
Enter 1st string: hello
Enter 2nd string: hello
Strings are equal
C Reverse String: strrev()
The strrev(string) function returns reverse of the given string. Let's see a simple example of strrev() function.
#include<stdio.h>
#include <string.h>
int main(){
char str[20];
printf("Enter string: ");
gets(str);//reads string from console
printf("String is: %s",str);
printf("\nReverse String is: %s",strrev(str));
return 0;
}
Output:
Enter string: Reshman
String is: Reshman
Reverse String is: namhseR
C String Lowercase: strlwr()
The strlwr(string) function returns string characters in lowercase. Let's see a simple example of strlwr() function.
#include<stdio.h>
#include <string.h>
int main(){
char str[20];
printf("Enter string: ");
gets(str);//reads string from console
printf("String is: %s",str);
printf("\nLower String is: %s",strlwr(str));
return 0;
}
Output
Enter string: RESHMAN
String is: RESHMAN
Lower String is: reshman
C String Uppercase: strupr()
The strupr(string) function returns string characters in uppercase. Let's see a simple example of strupr() function.
#include<stdio.h>
#include <string.h>
int main(){
char str[20];
printf("Enter string: ");
gets(str);//reads string from console
printf("String is: %s",str);
printf("\nUpper String is: %s",strupr(str));
return 0;
}
Output:
Enter string: reshman
String is: reshman
Upper String is: RESHMAN
|