The strings can be defined as the one-dimensional array of characters terminated by a null ( '\0' ).
There are two ways to declare a string in c language.
- By char array
- By string literal
There are two main differences between char array and literal.
- We need to add the null character '\0' at the end of the array by ourself whereas, it is appended internally by the compiler in the case of the character array.
- The string literal cannot be reassigned to another set of characters whereas, we can reassign the characters of the array.
Let's see the example of declaring string by char array in C language.
char ch[10]={'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', '\0'};
While declaring string, size is not mandatory. So we can write the above code as given below :
char ch[]={'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', '\0'};
We can also declare the string by string literal in C language.
Example:
In such case, '\0' will be appended at the end of the string by the compiler.
gets() and puts() : gets() and puts() function are used to read a line of string and to display the string respectively.
Example :
Output :
Passing Strings to Function :
Example :
Output :Output :
Most commonly used string functions :
Example:
char char[] = "neoogy.com";
In such case, '\0' will be appended at the end of the string by the compiler.
gets() and puts() : gets() and puts() function are used to read a line of string and to display the string respectively.
Example :
#include<stdio.h>
int main()
{
char name[20];
printf("Enter your name : ");
gets(name); // read string
printf("Your name is : ");
puts(name); // display string
return 0;
}
int main()
{
char name[20];
printf("Enter your name : ");
gets(name); // read string
printf("Your name is : ");
puts(name); // display string
return 0;
}
Output :
Enter your name : Neoogy
Your name is : Neoogy
Your name is : Neoogy
Passing Strings to Function :
Example :
#include <stdio.h>
void displayString(char str[]);
int main()
{
char str[50];
printf("Enter any string : ");
gets(str);
displayString(str); // Passing string to a function.
return 0;
}
void displayString(char str[])
{
printf("String Output : ");
puts(str);
}
void displayString(char str[]);
int main()
{
char str[50];
printf("Enter any string : ");
gets(str);
displayString(str); // Passing string to a function.
return 0;
}
void displayString(char str[])
{
printf("String Output : ");
puts(str);
}
Output :Output :
Enter any string : Welcome to Neoogy
String Output : Welcome to Neoogy
String Output : Welcome to Neoogy
Most commonly used string functions :
Functions | Description |
---|---|
strlen(string_name) | Calculates the length of string |
strrev(string) | Reverses a string |
strlwr(string) | Converts string to lowercase |
strupr(string) | Converts string to uppercase |
strcpy(destination, source) | Copies the contents of source string to destination string. |
strcat(first_string, second_string) | Concats or joins first string with second string. |
strcmp(first_string, second_string) | Compares the first string with second string. If both strings are same, it returns 0. |
Example :
#include <stdio.h>
int main()
{
char ch[20]={'W', 'e', 'l', 'c', 'o', 'm', 'e','\0'};
printf("Length of the string is : %d",strlen(ch));
return 0;
}
int main()
{
char ch[20]={'W', 'e', 'l', 'c', 'o', 'm', 'e','\0'};
printf("Length of the string is : %d",strlen(ch));
return 0;
}
Output :
Length of the string is : 7
« Previous Next »
0 Comments