Strings in C ?
A string is a sequence of characters enclosed within double quotes. A string constant is always terminated by a NULL character. A null terminated string is the only type of string defined by C language.
NULL-TERMINATED ----- char arr[5]={‘a’,’b’,’c’,’d’, ‘\0’}
the size of the character array containing the string is one more than the number of characters in the word.
String Input/ Output function
Header file---> #include<stdio.h>
- formatted input function.Ex:- scanf()
- formatted output functionEx:- printf()
- Un-formatted input functionEx:- gets()
- Un-formatted output functionEx:- puts()
scanf() | getsf() |
---|---|
The scanf() function can read input from keyboard and stores them according to the given format specifier. It reads the input till encountering a whitespace, newline or EOF. | On other hand gets() function is used to receive input from the keyboard till it encounters a newline or EOF. The whitespace is considered as a part of the input. |
In scanf() function white-space is not considered as input character and also it stops reading input from external source if any white-space is encountered in between. | On other hand in gets() function white-space is considered as input character and also it does stops reading input from external source if any white-space encountered in between it continue its reading from input source. |
scanf() function can read multiple values of different data types. | However on other hand gets() function will only get character string data |
String Handling function
Header file --> #include<string.h>
string | Explaination |
---|---|
Strlen(str)- | To find length of the string str |
Strcpy(dest,src) | Copies the source string src to destination string dest |
Strncpy(dest , src, n) | Copies at most n characters of the source string src to destination string |
Strcat(str1,str2) | Append string str2 to string str1 |
Strncat(str1,str2,n) | Append first n characters of string str2 to string str1 |
Strcmp(str1,str2) | Compare two string str1 and str2 |
Strrev(str) | Reverse the given string i.e abc output cba |
strupr(str)
strlwr(str) |
Convert lower case to upper case
Convert upper case to lower case |
Copyright © 2022 Shineskill Software Pvt. Ltd., All rights reserved.