string

A String is a collection or sequence of alphabets, numbers, and ant other special symbols. The compiler automatically assigns a null character ("\0") at the end of the string. Therefore size must be equal to a maximum number of characters plus one. Each character is stored one by one.C  does not support string data type, so we need to declare them as an array of characters.  

Syntax:

char string_variable_name[size];
char string_variable_name[size]="value";


Example:

char name[20]=" c programing";
char books[20];


    c                     p            r         o        g         r        a           m        i        n         g        /0   
    1   2    3     4    5   6     7    8     9    10    11    12  13
 when, we give any string , the string will stored like above table. 


sample program for string,


#include<stdio.h>
int main()
{
  char a[6]="hello";
  printf("string is:%s",a);
  return 0;



output:

 string is: hello 

Now, we see how to get an input string from users.

                      scanf("%s",string_name);

The "%s" control string can be used in scanf() statement to read a string from the terminal and the same may be used to write string into the terminal in printf() statement.
This the syntax for, get the string from the user. Here, we are not given the ampersand(&) symbol. 

#include<stdio.h>
int main()
{
  char a[10];
  printf("enter the sting:");
  scanf("%s",a);
  printf("string  is:%s", a);
 return 0;
}

output:

enter the string: cprogram
string is:cprogram




strlen() :


The strlen() function is used to count the number of characters present in a string. While calculating the string length by strlen() function "\0" character is not taken into consideration.

syntax:

var=strlen(string);

 
sample program for strlen()

#include<stdio.h>
int main(){
 char name[10];
 int len;
 printf("enter the str:");
 scanf("%s",&name);
 len=strlen(name);
 printf("length of the string is:%d",len);
 return 0;
}
output

enter the str: coder
length of the string is:5

strcpy() :


The strcpy() function is used to copy the contents of one string to another and it almost works like string assignment operators.

syntax:

strcpy(destination,source);

 
sample program for strcpy() function

#include<stdio.h>
int main()
{
  char name[20],copyname[20];
  printf("enter the name:");
  scanf("%s",&name);
  strcpy(copyname,name);
  printf("copied string is:%s",copyname);
  return 0;
}

output

enter the name:spc
copied string is:spc 
  


strcat():



The strcat() function is used to concatenate or combine two string together and forms a new concatenated string. After concatenation new string is stored insource string here it is a string1.
Normally, adding the two strings we are using an additional operator symbol. then, we stored our additional strings in another one new string. But here, this function do addition work at the same time it stores our strings in which string we are given in the firstly, the addition string will store in the first string.
 

syntax:addit

strcat(string1,string2);
 

sample program for strcat() function,

#include<stdio.h>
int main()
{
  char name1[2],name2[20];
  printf("enter the first string:");
  scanf("%s",name1);
  printf("enter the second string:");
  scanf("%s",name2);
  strcat(name1,name2);
  printf("full form of the string is:%s",name1);
  return 0;
}
output

enter the first string: computer
enter the second string: programer
full form of the string is: computerprogramer

strcmp() :


 strcmp():

this function is used to find two strings whether both the strings are the same or not. 

normally, it will compare the two strings character by character the comparison will be done until one end of the strings reached. Once you give the compiler option, if both strings are identical strcmp() it will return a value zero. Suppose if the strings are not the same means it will print the numerical number where and which character differs from other strings.


syntax:

     strcmp(string 1,string2);

program:

#include<stdio.h>
int main(){
       char str1[20],str2[20];
       int result;
       printf("enter the first str1:");
       scanf("%s",str1);
       printf("enter the second str:");
       scanf("%s",str2);
      result=strcmp(str1,str2);
     if(result==00{
        printf("both are same");
      }
    else{
      printf("both the strings are not same");
     }
    return 0;
  }

output:

enter the str1: abcde
enter the str2: abcde
both are same

strrev(), strlwr(), strupr():


strrev()

This function used to reverse the string.

it takes only one argument and it returns one argument.


syntax:

strrev(string);


program:

#include<stdio.h>
#include<string.h>
int main(){
   char str[20];
   printf("enter the string:");
   gets(str);
   printf("reverse of the string is:");
   strrev(str);
   return 0;

}


output:

enter the string:abcd
the reverse of the string is : dcba

strlwr() :

this function is used to convert all the characters to a lowercase character, then our converted string will be stored in our given string. the syntax for this function is,

                         strlwr(string);

strupr():

            this function is used to convert all the characters to uppercase character, then the result will be stored in the same string. the syntax for this function is,

               strupr(string)


PROGRAM:

#include<stdio.h>
#include<string>

int main(){
    char str1[20];
    printf("enter the string:");
    gets(str);
    printf("lowercase is:");
    strlwr(str);
    printf("upper case is:");
    strupr(str);
    return 0;

}

output:

enter the string: CoMpUtEr
lower case is: computer
upper case is: COMPUTER

No comments:

Post a Comment