Professional Documents
Culture Documents
CSE 105 - Lec 9-11 (String)
CSE 105 - Lec 9-11 (String)
1
Lec 9-11: String in C
PREPARED BY
SHAHRIAR RAHMAN KHAN
Lecturer, Department of CSE, MIST
Character Arrays
#include <stdio.h>
int main()
{
char values[]= {'h', 'e', 'l', 'l', 'o'};
int i;
printf("\n");
for (i = 0; i<9; i++)
{
printf("%c ", values[i]);
}
return 0;
}
» String is a sequence of characters that are treated as a single data item and
terminated by a null character '\0'.
» Remember that the C language does not support strings as a data type.
» A string is actually a one-dimensional array of characters in C language. These
are often used to create meaningful and readable programs.
Character Array h e l l o
String h e l l o \0
Input: Output:
hello h e l l o \0
hello
Note: When you take input of a string, you do not have to put the null character
explicitly. The compiler does it itself.
11/21/2022 LEC SHAHRIAR RAHMAN KHAN 14
String Input - Output
#include <stdio.h>
int main()
{
char values[6];
int i;
scanf("%s",values);
printf("%s is the string and %d the base address\n", values, values);
printf("%d the base address\n", &values[0]);
return 0;
}
h e l l o w o r l d \0
❏ You can access any element at any index just like a character array
Function Description
strlen(string_name) Can compute the length of the string
Can copy the content of a string to
Strcpy(string1 , string2 )
another
Is used to concatenate or join two
Strcat(dest, src)
strings
Strcmp(string1 , string2) Can compare two strings
Strlwr(string_name) Can convert the string to lowercase
Is used to convert the letters of string
Strupr(string_name)
to uppercase
Strrev(string_name) Is used to reverse the string
11/21/2022 37