Search This Blog

Monday, April 20, 2020

String explaination in C-Programming

                                       C-Programming

In this tutorial yon will learn:

String:

 

 

 A group of character is known as string .  C doesn't support string as a data type. We can create a string using character array.

Declaration of string:-

Declaring a string is a simple as declaring a one dimensional array. Below the basic syntax for declaring a string 
Data type             Array name [ size ] ;

for e.g:-         char     arr [ 30 ];  \\ declaration of string.

Initializing a string:-

A string can be initialized in different ways. We will explain this with the help of an example:-

1. char  str [ ]  =  "codeswithabhi" ;
2. char str [ 30 ] = " codeswithabhi" ;
3. char str [ ] = {'a' , 'b' , 'h' , 'i'} ;

Let see one example :-

// Write a program to accept values in a variable character array and print values from an array.

# include<stdio.h>
#include<conio.h>
void main()
{
char  str [ ] = "codeswithabhi" ;   // declare an initialize string
printf("%s", str);                        // print string
getch();
}

Output

codeswithabhi

String.h  as a header file .

The string.h header file have a some pre defined function\ library function which is very helpful in string's program  and make a code short.

There are some library function / predefined function of string in C language are:-

 1.   Strlen (  ) ; 

 Strlen stand for string lenght this function is used to count a character in a string it return a number as resultant . In this function length doesn't include null character. 

syntax:-

Variable = strlen ( string ) ;

for e.g :-

char na[ 20 ] = "abhi" ;
int l;
l=strlen(na);
printf("%d",l);

2.  Strrev (  ) ; 

  Strrev ( ) stand for string reverse this function is used to reverse a given string.

syntax       strrev ( string ) ;

for e.g :-

int main
char na [10] ={ 'a','b','h','i'};
printf("%s",na );
printf("%d",strrev(na));
 return(0);
}

Output

ihba

3.Strcpy (  ) ;  

 Strcpy stands for string copy this function is used to copy string into another string.

syntax:-

strcpy ( destination string , source string);

for e.g:

char str[10] = "abhi";
char str1 [10] ;
printf("%s",strcpy(str1,str));
return(0);
}

Output

abhi

4. Strcat (  ) ;  

 Strcat stand for string concatenation . When we combine two string add the character from one string to end of the another string this process is known as concatenation.

syntax:-           strcat( str , str1 ) ;

for e.g:--            

     char str [10]="codeswith";
                               char str1 [10]= "abhi" ;
                              strcat( str , str1 ) ;
                             printf("%s",str);       
                             printf("%s"str1);
                              return(0);

            Output

codeswithabhi

5. Strlwr ( ) ;   

 Strlwr (  )  stand for string lower case this function is used to convert a upper case string to the lower case.

6. Strcmp (  ) ;  

Strcpm  stand for compare string . It is used for to compare string this function compare two string on the basic of difference between ASCII value of the first string is greater than the ASCII value of second string it return negative value and if both string are equal then it return 0.

syntax :-         num = strcmp ( str1, str2) ;

for e.g:-

// write a program to check given string is identical or not 

#include<string.h>
#include<string.h>
int main () 
{
char str1 [10] ;
char str2 [10] ;
int cmp;
printf("enter first string ");
gets ( str1);
printf("enter second string");
  gets(str2) ;
cmp=strcmp(str1 , str2);
if(cmp==0)
{
printf("identical");
}
else 
{
printf("not identical");
}
return (0);
}

Output

enter first string      codeswithabhi
enetr second string    codeswithabhi
identical

7. Strupr ( ) ; 

 Strupr () stand for string upper case .This function used to convert given string into upper case.

syntax :-     strupr ( "string") ;

for e.g:

# include<stdio.h>
#include<string.h>
int main()
{
char str[100] ;
gets ( str) ;
printf("%s",strupr(str));
return(0);
}

// Write a program to find out the length of string with using library function

#include<stdio.h>
#include<conio.>
void main()
{
char  name [30];
int  i , l , count=0;
printf("enter your name ");
scanf("%s ", na);
for(i=0 ; name != '/0' ; i++)
{
count ++;
}
l=count ;
printf(" length of name is  =%d", l);
getch();
}

OUTPUT

enter your name  codeswithabhi
lenghth of name is = 13

Thank  you for reading my post ...

                                                   "codeswithabhi"                  


Mr. abhijeet dwivedi

Developer

Hey, I'm Abhijeet

0 comments:

Post a Comment

If you have any doubt, please let me know