You are on page 1of 4

C Programming Notes STRINGS A string is a collection of characters/ group of characters that ends with a null character.

A string is an array of characters that ends with a null character. Ex: India The null character is present at the end of string character array. The null character is represented by \0. The ASCII value of null character is zero. The purpose of null character is it indicates that, that is the end of string. Declaration of character array or string: Syntax: data type arrayname [size]; char arrayname [size]; Here arrayname indicates name of the character array and size indicates the number of characters it can store. Ex: char a[10]; char b[20]; Initialization of character array or strings: 1. Compile time initialization. 2. Run time initialization. Compile time initialization: char name[10]=PVPSIT; char name[ ]=PVPSIT; char name[10]={P,V,P,S,I,\0}; char name[ ]={P,V,P,S,I,\0}; Write a program to read a string and print the string in different ways #include<stdio.h> main() Run time initialization: { #include<stdio.h> char name[]="India"; main() int i; { for(i=0;i<5;i++) char str[10];int i; printf("%c",name[i]); for(i=0;i<10;i++) for(i=0;name[i]!='\0';i++) scanf("%c",&str[i]); printf("%c",name[i]); puts(str); printf("%s",name); getch(); puts(name); } } Output: India India India India NOTE: If the no of characters entered in the string is less than the string size, then compiler places a null character at the first empty location in the string. Ex: char str[10]=PVP; STRING LIBRARY FUNCTIONS: 1. strlen() ---- string length 2. strcpy()---- string copy 3. strcat() ---- string concatenation 4. strrev() --- string reverse 5. strcmp()--- string comparison 6. strlwr() --- string lower 7. strupr()--- string upper

PREPARED BY P.RAVI PRAKASH ASSISTANT PROFESSOR@PVPSIT

C Programming Notes strlen(): This function is used to find the length of string. i.e no of characters present in it; This function return no of characters in the string expect null character. Syntax: strlen(string); Write a program to find length of string Write a program to find length of string with using library function out using library function #include<stdio.h> #include<stdio.h> #include<string.h> main() main() { { char str[20]; char str[20]; int i,length=0; int length; printf("\nEnter a string"); printf ("\nEnter a string"); gets(str); gets(str); for(i=0;str[i]!='\0';i++) length=strlen(str); length++; printf("Length of string=%d",length); printf("Length of string=%d",length); } } OUTPUT Enter a string : Ravi Length of string=4 strcpy () :- This function is used to copy contents of one string into another string. Syntax:- strcpy(string1,string2); string1:Destination string2:source Write a program to copy one string into another string using library function. #include<stdio.h> #include<string.h> main() { char str[10],temp[10]; printf(\nEnter a string:); gets(str); strcpy(temp,str); puts(str); puts(temp); } OUTPUT Enter a string: Ravi Prakash Ravi Prakash Ravi Prakash strcat():- This function is used to concatenate two string values i.e. contents of one string are attached with the contents of another string. Syntax:- strcat(string1,string2); Here contents of string2 are append to contents of string1. The string concatenation function replaces the null character in the first string and appends the second string from that point onwards. Write a program to concatenate two strings using library function. #include<stdio.h> #include<string.h> main() { char s1[30]=ABC,s2[30]=DEF,s3[30]=GHI; strcat(s1,s2); strcat(s1,s3); PREPARED BY P.RAVI PRAKASH ASSISTANT PROFESSOR@PVPSIT

C Programming Notes puts(s3); puts(s2); puts(s1); } strrev():-this function is used to reverse the contents of given string Syntax:- strrev(string); /* WAP CHECK IF A GIVEN STRING IS A Write a program to reverse the PALINDROME */ contents of a string using library #include<string.h> function #define size 10 #include<string.h> void main() main() { { char strsrc[size],strtmp[size]; char str[20]; printf("\n Enter String:= "); printf(Enter a string :); gets(strsrc); scanf(%s,str); Output: strcpy(strtmp,strsrc); printf(Before reverse:); strrev(strtmp); puts(str); Enter if(strcmp(strsrc,strtmp)==0) strrev(str); String:MOM printf("String is palindrome"); printf(After reverse); else puts(str); String is printf("String is not a palindrome"); } palindrome OUTPUT getch(); ANIL } LINA strcmp():- This function is used to compare two string values whether they are equal, which one is bigger or smaller string. This function returns zero value if both strings are equal, it returns less than zero value or negative value, if the first string is less than second string. it returns greater than zero value if the first string is greater than second string. Syntax:- strcmp(string1,string2); It performs comparison on the two strings based on the ASCII value of the characters present in the string. Write a program to compare two strings using string library functions #include<stdio.h> #include<string.h> main() { char str1[20],str2[20];int n; printf(Enter string1:); gets(str1); printf(Enter string2:); gets(str1); OUTPUT: n=strcmp(str1,str2); Enter string1:ANIL if(n==0) Enter string2:KUMAR printf(Both strings are equal:); String1 is less than string2 else if(n>0) Enter string1:MOM printf(string1 is greater than string2); Enter string2:MOM else printf(String1 is less than string2:); Both strings are equal } strlwr():- This function is used to convert uppercase letters in a string into lowercase. Syntax:- strlwr(string); OUTPUT: GHI DEF ABCDEFGHI

PREPARED BY P.RAVI PRAKASH ASSISTANT PROFESSOR@PVPSIT

C Programming Notes Write fa program to convert a string into lowercase using library function. #include<string.h> main() OUTPUT: { char str[20]; Enter a string:ANIL Before conversion: ANIL printf(\nEnter a string:); scanf(%s,str); After conversion: anil printf( Before conversion:); puts(str); strlwr(str); printf(After conversion:); puts(str); } strupr():- This function is used to convert lowercase letters in a string into uppercase. Syntax:- strupr(string); Write a program to convert a string into uppercase using library function. #include<string.h> main() OUTPUT: { Enter a string:anil char str[20]; Before conversion: anil printf(\nEnter a string:); After conversion: ANIL scanf(%s,str); printf( Before conversion:); puts(str); strupr(str); printf(After conversion:); puts(str); } Write a program to write the given number in words. main() { case 5:printf("five\t");break; int a[10],n,i,size; case 6:printf("six\t");break; printf("enter the number\n"); case 7:printf("seven\t");break; scanf("%d",&n); case 8:printf("eight\t");break; i=0; case 9:printf("nine\t");break; while(n>0) } { } a[i]=n%10; } n=n/10; OUTPUT: i++; enter the number } 2345 size=i; two three four five for(i=size-1;i>=0;i--) { switch(a[i]) { case 0:printf("zero\t");break; case 1:printf("one\t");break; case 2:printf("two\t");break; case 3:printf("three\t");break; case 4:printf("four\t");break; PREPARED BY P.RAVI PRAKASH ASSISTANT PROFESSOR@PVPSIT

You might also like