What is string in c?
Hello, guys today we are going to see detailed information in C. The below elucidation will make your whole concept about Strings crystal clear.
So firstly let’s start by defining strings.
Definition:
A string is defined in a single character. Anything inside of double quotes is a string. This can be any number of characters. A string in C is ended with a null character \0.
So now the question comes how to make strings in c.
To define a string we simply declare one dimension array.
Syntax:
Char name[size of array]
Where name is the name of string variable and size of an array is used to define the length of the string.
Now we will see how to print a string
How to print a string
Input
#include <stdio.h>
int main()
{
char a[500] = "Scientific World";
printf("%s", a); //here %s is format specifier
return 0;
}
Output
Scientific World
How to find the length of a string.
Guys, now we will see four simple ways to find the length of the string
- Using Standard Method
- Using Function
- Using Recursion
- Using String Library Function
Using standard Method in C
We entered the string using the gets() function and store it in the character array. Loop repeat from the first character to the last character of the array. Then Print the value which is the length of the string.
Input 1:
#include <stdio.h>
int main()
{
char s[1000];
int i;
printf("Enter any string: ");
gets(s);
for(i=0; s[i]!='\0'; i++);
printf("Length of '%s' = %d",s,i);
return 0;
}
Output 1:
Enter any string: Science
Length of 'Science' = 7
Using Function
The main() function calls the length() function by passing a string as an argument. The function length() has the code to find the length of the string.
Input 2:
#include <stdio.h>
int length(char *s)
{
int i;
for(i=0; s[i]!='\0'; i++);
return i;
}
int main()
{
char s[1000];
int len;
printf("Enter any string: ");
gets(s);
len=length(s);
printf("Length of '%s' = %d",s,len);
return 0;
}
Output 2:
Enter any string: School
Length of 'School' = 6
Using Recursion
The main() calls the recursive function length() by passing string s and where i=0 as arguments.
The recursive function checks the character is null or not.
If the character is found to be null then returns the value i. If the character is null then its return I and suppose the character is not null then the function calls itself by increasing i value as length(s,++i). The function calls recursively until s[i] is null. The function call until s[i] is null. Then the main() function prints i value.
Input 3:
#include <stdio.h>
int length(char *s,int i)
{
if(s[i]=='\0')
return i;
length(s,++i);
}
int main()
{
char s[1000];
int len;
printf("Enter any string: ");
gets(s);
len=length(s,0);
printf("Length of '%s'= %d",s,len);
return 0;
}
Output 3:
Enter any string: Computer
Length of 'Computer'= 8
Using String Library Function
Here we are using the library function strlen() to find the length of the string. The main() calls the strlen() predefined function by passing string s as an argument. The strlen() calculates the length of the string and the value assigned to the variable length. Then Print the length value.
Input 4:
#include <stdio.h>
#include <string.h>
int main()
{
char s[1000];
int length;
printf("Enter any string: ");
gets(s);
length=strlen(s);
printf("Length of '%s'= %d",s,length);
return 0;
}
Output 4:
Enter any string: Java
Length of 'Java'= 4
Now we will discuss some questions based on string
Question based on strings
How to reverse the string in c using the Recursion function
Input 1
#include <stdio.h>
void reverse(); //Function declaration
int main() {
printf("Enter string: ");
reverse();
return 0;
}
//we are defining the function here
void reverse()
{
char c;
scanf("%c", &c);
if (c != '\n') {
reverse();
printf("%c", c);
}
}
Result 1
Enter string: Scientific World
dlroW cifitneicS
Write a C program to sort a string array in ascending order
Input 2
#include <stdio.h>
#include <string.h>
int main()
{
char str[100],ch;
int i,j,l;
printf("Input the string : ");
fgets(str, sizeof str, stdin); //
l=strlen(str);
for(i=1;i<l;i++)
for(j=0;j<l-i;j++)
if(str[j]>str[j+1])
{
ch=str[j];
str[j] = str[j+1];
str[j+1]=ch;
}
printf("After sorting: ");
printf("%s",str);
}
Output
Input the string : Apple
After sorting:
Aelpp
Commonly Used String Functions in c
- strlen() – It calculates the length of a string
- strcpy() - It copies a string
- strcmp() – It used to compare two strings
- strcat() - concatenates two strings
Important Points
- A string is a sequence of characters stored in an array.
- A string is confined in double quotation marks.
- A string must be declared before a program.
- String manipulators are stored in <string.h> header file.
Facts:
- C is a structured programming language that allows the programmer to break his codes into smaller Parts to improve the legibility of the code and hence make the program less redundant and simple.
Thanks for reading.
If you have any doubt or anything you want to ask, ask freely in the comment section. We are always here for you.
Comments
Post a Comment