A string is a sequence of charachters that is treated as a single data item.
Examples:
"Hello World"
"Enter the Number:"
"Sum = 12"
char string_name[size];
char city[10];
char message[15];
type array-name[size] = { list of values };
char city[7] = "Mumbai";
char city[7] = {'M','u','m','b','a','i','\0'};
Note:
Size can be ommited. It will be computed from the number of elements in the initialization + 1 for NULL ($\0$) charachter.
char message[] = "Hello"; // size is 6
char message[] = {'a','e','i','o','\0'}; // size is 5
Null charachter $\0$ tells the compiler that the string has ended. The last element of the string should always be a NULL.
Note:
Compile time initialization can be partial. That is the number of initializers can be less than the declared size. In such cases the remaining elements are initialized with NULL ($ \0 $) in a string.
char message[5] = {'a','e' }; // 'a','e','\0','\0','\0'
char city[8] = "Delhi"; // 'D','e','l','h','i','\0','\0','\0'
$ \%s $ and $ \%[ ] $ format specifiers is used to read data from terminal using scanf
char country[20];
scanf("%s",country);
scanf("%[^\n]",country);
Note: $ \%s $ will read a word only. It stops reading when user enters space.
In scanf when using charachter arrays as second parameter, & is not required.
char country[20];
gets(country);
gets can read sentences.
WAP to get a string from user and display it on screen.
WAP to check if there is a vovel charachter in the entered string.
Write a program in C to find the length of a string without using library function.
Write a program in C to print individual characters of string in reverse order without using library function.
Write a program in C to count the total number of words in a string.
Write a program in C to copy one string to another stringwithout using library function.
Write a program in C to remove special characters in String and keep only Alphabets and numbers.
Write a program in C to check if the entered string is palindrome or not.
printf("%s",a);
puts(a);
#include <string.h>
The strlen(a) function computes the number of bytes in the string a, not including the terminating null byte.
#include <string.h>
char a[20] = "Mumbai";
printf("%d",strlen(a)); // 6
The strcpy() function copies string from a to string b by replacing the contents of b.
#include <string.h>
char a[20] = "Mumbai",b[20];
strcpy(b,a);
puts(b);
strcpy(destination,source);
The strcat() function joins two string together. It joins the contents of string b to string a along with the null charachter.
#include <string.h>
char a[20] = "Hello ",b[20]="World";
strcat(a,b);
puts(a);
The strcat() function checks if both the strings are equal.
#include <string.h>
char a[20] = "Hello",b[20]="Hello";
if(strcmp(a,b)==0){
printf("Same");
}else{
printf("Different");
}
strlwr( ) function converts a given string into lowercase.
#include <string.h>
char a[20] = "HEllo";
strlwr(a); // hello
puts(a);
strupr( ) function converts a given string into uppercase.
#include <string.h>
char a[20] = "HEllo";
strupr(a); // HELLO
puts(a);
strrev( ) function reverses a given string in C language
#include <string.h>
char a[20] = "hello";
strrev(a); // olleh
puts(a);
#include <stdlib.h>
This function returns the converted integral number as an int value. If no valid conversion could be performed, it returns zero.
#include <string.h>
int val;
char a[20] = "1900";
val = atoi(a);
printf("%d",val); // 1900
This function returns the converted integral number as an int value. If no valid conversion could be performed, it returns zero.
#include <string.h>
float val;
char a[20] = "19.3";
val = atof(a);
printf("%f",val); // 19.3
Write a program in C to reverse a string.
To create a list of strings. Multiple strings in one variable.
char names[5][20] = {
"Amit",
"Arun",
"Junaid",
"Shibu"
};
printf("%s",names[0]); // Amit
printf("%s",names[2]); // Junaid