6. String

 The collection of characters is called string .

Syntax:

Char name[] = “INDIA”;


The c compiler inserts the NULL(\0) character automatically at the end of the string,so initialization of the NULL character is not essential.






Write a program to display the out when the account of NULL character is not considered.


#include<stdio.h>

#include<string.h>


void  main (){

char name [6] ={'s','A','N','I','K','A'};

printf("name = %s",name);

}

 

Output:


name = sANIKA



Write a program to print “WELCOME” “To” “SANIKA” by using different formats of initialization of the array. 



#include<stdio.h>

#include<string.h>

#include<conio.h>

void  main (){

char arr1 [10] = {'W','E','L','C','O','M','E'};

char arr2 [10] = "to";

char arr3 [10] = {{'S'},{'A'},{'N'},{'I'},{'K'},{'A'}};

printf("\nArray1 = %s",arr1);

printf("\nArray2 = %s",arr2);

printf("\nArray3 = %s",arr3);

}

 


Output :


Array1 = WELCOME

Array2 = to

Array3 = SANIKA



Use the while loop and print out the elements of the character aaray


#include<stdio.h>

#include<string.h>

#include<conio.h>

void  main (){

char text [] = "HAVE A NICE DAY";

int i=0;

while(i<=15){

printf("%c",text[i]);

i++;

}

}

 

Output :


HAVE A NICE DAY



DIFFERENT FOUR TYPES OF STRING 


  1. strlen() = Determines length of a string 


  1. Strcpy ()= copies a string source to destination .


  1. strcmp() = compares to string.


  1. Strcat ()= Appends source string to destination string.



  1.  strlen() function 


This function counts the number of characters in a given string .



Write a program to count the number of characters in a given string.


#include<stdio.h>

#include<string.h>

#include<conio.h>

void  main (){

char text [20];

int len;

printf("type the string \n");

gets(text);

len = strlen(text);

printf("Length of the string = %d",len);

}

 

Output :


type the string

SANIKA

Length of the string = 6




Write a program to read a name through the keyboard .Determine the length of the string and find its equivalent ASCII  value.


#include<stdio.h>

#include<string.h>

#include<conio.h>

void  main (){

char name [20];

int i,len;

printf("enter your name \n");

gets(name);

len = strlen(name);

printf("your name is %s  & ",name);

printf("Length of the string = %d",len);

printf("\n name & its ASCII equivalent  ");

for(i=0;i<len;i++)

printf("\n %c\t\t%d",name[i],name[i]);

getche();

}

 

Output :


enter your name

SANIKA

your name is SANIKA  & Length of the string = 6

 name & its ASCII euivalent

 S              83

 A              65

 N              78

 I              73

 K              75

 A              65





Strcpy () function 


This function copies the contents of one string to another 

This format of strcpy () is strcpy (s2,s1).


Where 

S1 = is source string 

S2 = is a destination string 


S1 is copied to s2.




Write a program to copy contents of one string to another string by using strcpy().


#include<stdio.h>

#include<string.h>

#include<conio.h>

void  main (){

char ori[20],dup[20];

printf("Enter your name : ");

gets(ori);

strcpy(dup,ori);

printf("Original string : %s ",ori);

printf("\nDuplicate string : %s ",dup);

}

 

Output :


Enter your name : SANIKA

Original string : SANIKA

Duplicate string : SANIKA





3)stricmp() function 

 

This function compares the two string .The characters of the string may be in lowercase or upper case the function does not discriminating between them  i.e. this function compares two string without case . 

If the string are same it returns zero otherwise non-zero value 




Write a program to compare the two strings using the stricmp() function .if the strings are identical display . The two strings are Identical otherwise The strings  are different .


#include<stdio.h>

#include<string.h>

#include<conio.h>

void  main (){

char sr[10], tar[10];

int diff;

printf("Enter string (1): ");

gets(sr);

printf("Enter string (2): ");

gets(tar);

diff=strcmp(sr,tar);

if(diff==0){

printf("The two strings are indentical ");

}

else{

printf("The two strings are different  ");

}

getche();


}

 

Output:


Enter string (1): HELLO

Enter string (2): hello

The two strings are different


4)strcat() function 


This function does nothing but join the two strings .

The format of this string is (text1,text2).




Write a program  to join second string at the end of the first string using strcat() function .


#include<stdio.h>

#include<string.h>

#include<conio.h>

void  main (){

char text1[20],text2[30];

puts("enter the text 1");

gets(text1);

puts("enter the text 2");

gets(text2);

 strcat(text1 ," ");

 strcat(text1,text2);

 printf("%s",text1);

}

 

Output :


enter the text 1

I AM

enter the text 2

BEST

I AM BEST





Write a program to concatenate two strings without use of standard function .


#include<stdio.h>

#include<string.h>

void  main (){

char name [50],fname[50],sname[50],lname[50];

int i,j,k;

printf("First name\n ");

gets(fname);

printf("Second name\n");

gets(sname);

printf("Last name \n");

gets(lname);


for(i=0;fname[i]!='\0';i++)

name[i] = fname[i];

name[i] = ' ';


for(j=0;sname[j]!='\0';j++)

name[i+j+1] = sname[j];

name[i+j+1] = ' ';


for(k=0;lname[k]!='\0';k++)

name[i+j+k+2] = lname[k];

name[i+j+k+2] = '\0';


printf("\n\n");

printf("FULL NAME \n");

printf("%s",name);

getchar();


}

 

Output :


First name

 SANIKA


Second name

PANDITRAO


Last name

NARWADE



FULL NAME

SANIKA PANDITRAO NARWADE


Post a Comment

Previous Post Next Post