Strings in C
Strings in C
What is String
Strings are actually one-dimensional array of
characters terminated by a null character '\0'.
Declaration of Character:
char a;
Declaration of String:
char myString[10];
The characters after the null character are ignored.
'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.
String Initialization
Initialization Syntax:
- char str [] = { 'H','A','E','S', 'L', 'E', 'R', '\0' } ;
- char myString[10] = “Hello”;
- char myString[] = “Initial value”;
where \0 = null character, denotes end of the string.
OUTPUT:
Enter string:Hello World
String is:Hello
Reading string from user
Using gets()
Takes a string from standard input and assigns it to a
character array.
Can read multi word strings.
Syntax:
Example:
char str[30]; char str[30];
printf(“Enter string:”);
gets(str); gets(str);
printf(“String is:%s”,str);
OUTPUT:
Enter string:Hello World
String is:Hello World
Reading string from user
Using getchar()
getchar() as the name states reads only one character at
a time.
In order to read a string, we have to use this function
repeatedly until a terminating character is encountered.
The characters scanned one after the other have to be
stored simultaneously into the character array.
Syntax: for single character
char str;
str=getchar();
Reading string from user
Using getchar()
int main()
{
char str[50], ch; int i;
printf("Enter a string: ");
i = 0;
ch = getchar ();
while(ch!='\n')
{
str[i] = ch;
i++;
ch = getchar();
}
str[i] ='\0';
printf("Entered string is: %s", str);
return 0;
}
Printing strings
Using printf()
Using printf, a string can be displayed using %s format
specifier.
char str[10]=“Hello”;
printf(“%s”,str);
Using puts()
It just takes its parameter as the string to be printed.
char str[20]=“Hello World”;
puts(str);
Printing strings
Using putchar()
putchar() as the name states prints only one character at a
time.
In order to print a string, we have to use this function
repeatedly until a terminating character is encountered.
i = 0;
while(str[i]!='\0')
{
putchar( str[i] );
i++;
}
String Opertions-string.h
strlen()
returns number of characters in string
strcpy()
copy one string into another
strcat()
append one string onto the right side of the other
strcmp()
compare alphabetic order of two strings
strrev()
reverse the given string
strlwr()
converts the given string to lowercase
strupr()
converts the given string to uppercase
strlen()
The strlen() function takes a string as an argument and
returns its length.
Example:
char a[20]="Program";
printf("Length of string a = %ld \n",strlen(a));
strcpy()
strcpy(target, source)
Copies source string into target string
Example:
char str1[10]= "awesome";
char str2[10];
strcpy(str2, str1);
puts(str1);
puts(str2);
strcat()
The function strcat() concatenates two strings.
Example:
char str1[] = "This is ", str2[] = "programiz.com";
//concatenates str1 and str2 and resultant string is stored in
str1. strcat(str1,str2);
puts(str1);
puts(str2);
strcmp()
The strcmp() compares two strings character by character and
returns an integer.
If the first character of two strings is equal, the next character of
two strings are compared.
This continues until the corresponding characters of two strings
are different or a null character '\0' is reached.