-->

NEB24:explanation of string handling functions

Ans-
String--
           It means series of characters used in our program. We can also use digits with characters. They also act as string or a part of it. It is used when we input our name or address or somewhere their combination.

Syntax
char identifier[value];
or char identifier[value][value];

We can use he upper one if we want to input single string then. And if we want to input multiple values then we use lower one(called 2-dimensional array in string).

For example,
char name[100];
gets(name)
Here, the variable accepts value having maximum characters 100. We can also write ,
char name[10]="kathmandu";
IT stores the value in following way.

It stores values from cell location '0' and ends at null character called escape sequence. This is used to show that there is no more character beyond that. This null character is used with strings only. For  numbers, it is not used.
                        Similarly, if we want to store multiple string with same name then one-dimensional is not applicable. We use two dimensional string.
We declare:

char name[5][50];
It means, we are going to store any five names having optimum 50 characters.
Let's look below. If we have five names :RAM,SHYAM,Nita then it would like

so sequentially it stores data in different row. We use loop to execute it with different data.
Or, instead of inputting string, if we initialize strings then it can be written as
  char name[5][50]={"RAM","Shyam","Nitu"};

String handling functions:-There are some in-built functions,  we can say in-built small program. It is used to make our program easier and faster.
      Let's know about some.
1) strlen(string);-It is used to get length of given string.

 syntax:
identifer=strlen(string);
For example,
#include<stdio.h>
#include<string.h>
void main()
{
char string[100]="apple";
int k;
k=strlen(string);
printf("length=%d",k);
}
It returns 5. It also counts white spaces present in a string.
If we want want , we can also input string using scanf() or gets().

2)strrev():- It reverses the string given by us.

syntax:- strrev(string);
example:

#include<stdio.h>
#include<string.h>
void main()
{
char string[100]="apple";
printf("reverseed string=%s",strrev(string));
}

We get output "elppa". Instead of putting in output line, we can put it before printf.

3)strlwr(): This function is used to convert given string into lower case.
syntax:-
strlwr(string);
Example;
#include<stdio.h>
#include<string.h>
void main()
{
char string[100]="APPLE";
printf("string in lowercase=%s",strlwr(string));
}

It gives us "apple" as an  output.

4)strupr():This string function is used to convert all characters of  string into upper case.

syntax:
strupr(string);

example:-
#include<stdio.h>
#include<string.h>
void main()
{
char string[100]="apple";
printf("string in uppercase=%s",strupr(string));
}

We get APPLE as an output after execution.

5)strcpy();- We use this function to copy one string to another. while copying, one replaces another.
Syntax:-
strcpy(string1,string2);
or strcpy(destination,source);

When we copy string, this is done from source to destination (string2 to string1). If first is empty then there would not be over-writing.

Example;
#include<stdio.h>
#include<string.h>
void main()
{
char string1[100]="APPLE";
char string2[100]="grapes";
printf("copied string =%s",strcpy(string1,string2));
}

output-
We get here string1=grapes. Because 'grapes' is copied to firs string named "APPLE". We can also write in reverse order.

Like these all, we have many other string handling functions like, strcat(),strchr(),strdup(),strset() etc.





No comments:

Post a Comment