Вы находитесь на странице: 1из 0

C Programming Lecture Notes

VVSV

108
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]=RVRJC;
Char name[ ]=RVRJC;
Char name[10]={R,V,R,J,C};
Char name[ ]={R,V,R,J,C};

Write a program to read a string and print the string in different ways
#include<stdio.h>
main()
{
char name[]="India";
int i;

for(i=0;i<5;i++)
printf("%c",name[i]);

for(i=0;name[i]!='\0';i++)
printf("%c",name[i]);

printf("%s",name);

puts(name);
}
Output:

India India India India

Run time initialization:
#include<stdio.h>
main()
{
char str[20];
for(i=0;i<10;i++)
scanf(%c,&str[i]);
puts(str);

scanf(%s,&str);
puts(str);
C Programming Lecture Notes
VVSV

109

scanf(%s,str);
puts(str);

gets(str);
puts(str);
}

NOTE: If the no of characters entered in the string is lessthan the string size, then
compiler places a null character at the first empty location in the string.
Ex: char str[10]=RVR;

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


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);

1. Write a program to find length of string using library function
#include<stdio.h>
#include<string.h>
main()
{
char str[20];
int length;
printf ("\nEnter a string");
gets(str);
length=strlen(str);
printf("Length of string=%d",length);

}

OUTPUT
Enter a string : kishore
Length of string=7

2. Write a program to find length of string with out using library function

#include<stdio.h>
main()
{
char str[20];
int i,length=0;
printf("\nEnter a string");
gets(str);

for(i=0;str[i]!='\0';i++)
length++;
printf("Length of string=%d",length);

}

OUTPUT
Enter a string : krishna
C Programming Lecture Notes
VVSV

110
Length of string=7

strcpy () :- This function is used to copy contents of one string into another string.
Syntax:- strcpy(string1,string2);
Destination source
3. 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: Anil kumar
Anil kumar
Anil kumar

4. Write a program to copy one string into another string without using library function.
#include<stdio.h>
main()
{
char str[10],temp[10];
int i;
printf(\nEnter a string:);
gets(str);
for(i=0;str[i]!=\0;i++)
{
temp[i]=str[i];
}
temp[i]=\0;
puts(str);
puts(temp);
}

OUTPUT
Enter a string: Anil kumar
Anil kumar
Anil kumar

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.

5. 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);
puts(s3);
puts(s2);
C Programming Lecture Notes
VVSV

111
puts(s1);
}
OUTPUT
GHI
DEF
ABCDEFGHI

6. Write a program to concatenate two strings with out using library function.
#include<stdio.h>
main()
{
char s1[30]=ANIL,s2[30]=KUMAR,s3[30]=ARUN;
int i,j;
for(i=0;s1[i]!=\0;i++)
{
}
for(j=0;s2[j]!=\0;j++)
{
s1[i]=s2[j];
i++;
}
for(j=0;s3[j]!=\0;j++)
{
s1[i]=s3[j];
i++;
}
s1[i]=\0;
puts(s1);
}
OUTPUT
ANILKUARARUN

strrev():-this function is used to reverse the contents of given string.
Syntax:- strrev(string);
7. Write a program to reverse the contents of a string using library function
#include<string.h>
main()
{
char str[20];
printf(Enter a string :);
scanf(%s,str);
printf(Before reverse:);
puts(str);
strrev(str);
printf(After reverse);
puts(str);
}
OUTPUT
ANIL
LINA

8. Write a program to reverse the contents of a string without using library function
main()
{
char str[20],temp;
int i,j,length;
puts(Enter a string :);
scanf(%s,str);
length=strlen(str);
for(i=0,j=length-1;i<j;i++,j--)
{
temp=str[i];
str[i]=str[j];
C Programming Lecture Notes
VVSV

112
str[j]=temp;
}
puts(str);
}
OUTPUT
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.

9. 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);
n=strcmp(str1,str2);
if(n==0)
printf(Both strings are equal:);
else if(n>0)
printf(string1 is greater than string2);
else
printf(String1 is less than string2:);
}
OUTPUT:
Enter string1:ANIL
Enter string2:KUMAR
String1 is less than string2

10. Write a program to compare two strings with out using string library functions
#include<stdio.h>
main()
{
char str1[20],str2[20];
int i,j;
printf(Enter string1:); gets(str1);
printf(Enter string1:); gets(str1);
i=j=0;
while(str[i]!=\0 || str2[j]!=\0)
{
if(str1[i]>str2[j])
{
printf(%s > %s,str1,str2);
exit(0);
}
else if(str1[i]<str2[j])
{
printf(%s < %s,str1,str2);
exit(0);
}
else
{
i++;
C Programming Lecture Notes
VVSV

113
j++;
}
}
printf(Both Strings are equal:);
}
OUTPUT:
Enter string1:ANIL
Enter string2:KUMAR
String1 lessthan string2.

strlwr():- This function is used to convert uppercase letters in a string into lowercase.
Syntax:- strlwr(string);

11. Write a program to convert a string into lowercase using library function.
#include<string.h>
main()
{
char str[20];
printf(\nEnter a string:);
scanf(%s,str);
printf( Before conversion:);
puts(str);
strlwr(str);
printf(After conversion:);
puts(str);
}
OUTPUT:
Enter a string:ANIL
Before conversion: ANIL
After conversion: anil
12. Write a program to convert a string into lowercase using library function.
#include<string.h>
main()
{
char str[20]; int i;
printf(\nEnter a string:);
scanf(%s,str);
printf( Before conversion:); puts(str);
for(i=0;str[i]!=\0;i++)
if(isupper(str[i])
str[i]=tolower(str[i]);
printf(After conversion:); puts(str);
}
OUTPUT:
Enter a string:ANIL
Before conversion: ANIL
After conversion: anil


13. Write a program to convert a string into lowercase without -using library function.
#include<stdio.h>
main()
{
char str[20]; int i;
printf(\nEnter a string:);
scanf(%s,str);
printf( Before conversion:); puts(str);
for(i=0;str[i]!=\0;i++)
if(str[i]>=A && str[i]<=Z)
str[i]=str[i]+32;
printf(After conversion:); puts(str);
}
OUTPUT:
C Programming Lecture Notes
VVSV

114
Enter a string:ANIL
Before conversion: ANIL
After conversion: anil

strupr():- This function is used to convert lowercase letters in a string into uppercase.
Syntax:- strupr(string);

14. Write a program to convert a string into uppercase using library function.
#include<stdio.h>
#include<string.h>
main()
{
char str[20];
printf(\nEnter a string:);
scanf(%s,str);
printf( Before conversion:);
puts(str);
strupr(str);
printf(After conversion:);
puts(str);
}
OUTPUT:
Enter a string:anil
Before conversion: anil
After conversion: ANIL

15. Write a program to convert a string into uppercase using library function.
#include<stdio.h>
#include<string.h>
main()
{
char str[20]; int i;
printf(\nEnter a string:);
scanf(%s,str);
printf( Before conversion:); puts(str);
for(i=0;str[i]!=\0;i++)
if(islower(str[i])
str[i]=toupper(str[i]);
printf(After conversion:); puts(str);
}

OUTPUT:
Enter a string:anil
Before conversion: anil
After conversion: ANIL


16. Write a program to convert a string into uppercase without -using library function.
#include<stdio.h>
main()
{
char str[20]; int i;
printf(\nEnter a string:);
scanf(%s,str);
printf( Before conversion:); puts(str);
for(i=0;str[i]!=\0;i++)
if(str[i]>=a && str[i]<=z)
str[i]=str[i]-32;
printf(After conversion:); puts(str);
}
OUTPUT:
Enter a string:anil
Before conversion: anil
C Programming Lecture Notes
VVSV

115
After conversion: ANIL

Passing string to a function
Function prototype:-
Return-type function name(char string name[size]); or
Return-type function name(char [size]); or
Return-type function name(char []);
Calling function:-
Function name(string name);
Called function:-
Return-type function name(char string name[size])
{


}
here size is optional.



Write a program to read and print string values using functions.

#include<stdio.h>
void read(char [20]);
void display(char []);

main()
{
char str[20];
read(str);
display(str);
}

void read(char s[20])
{
printf("\nEnter a string:");
gets(s);
}

void display(char s[])
{
printf("The string is:");
puts(s);
}

output:
Enter a string: krishna
The string is: Krishna

Write a program to find whether the given string is palindrome or not?

#include<stdio.h>
void read(char [20]);
void display(char []);
void palindrome(char []);

main()
{
char str[20];
read(str);
display(str);
palindrome(str);
}

C Programming Lecture Notes
VVSV

116
void read(char s[20])
{
printf("\nEnter a string:");
gets(s);
}

void display(char s[])
{
printf("The string is:");
puts(s);
}

void palindrome(char s[20])
{
int length,c=0,i,j;
length=strlen(s);
for(i=0,j=length-1;i<=j;i++,j--)
if(s[i]==s[j])
c++;
if(c>=length/2)
printf("The string is palindrome:");
else
printf("Not palindrome");
}

output:
Enter a string: madam
The string is: madam
The string is palindrome

Enter a string: sai krishna
The string is: sai Krishna
Not palindrome


Double dimensional character arrays:-
Collection of single character arrays

Syntax:- char variable name[size1][size2]; or
char variable name[row][column];

Here variable name indicates name of the double dimensional character array. Here row size indicates no
of strings it can store and column size indicates the size of each string.

Initialization of double dimensional character arrays:-
Compile time initialization
Run time initialization.
Compile time initialization:-

Char names[10][20]={{I,N,D,I,A,\0},
{A,S,I,A,\0},
{E,U,R,O,P,E,\0}};
char names[][20]={{I,N,D,I,A,\0},
{A,S,I,A,\0},
{E,U,R,O,P,E,\0}};
char names[10][20]={INDIA,ASIA,EUROPE};

char names[][20]={INDIA,ASIA,EUROPE};

Write a program to print the contents of double dimensional character array in different ways.

#include<stdio.h>
main()
C Programming Lecture Notes
VVSV

117
{
char names[][20]={"ASIA","EUROPE","INDIA"};
int i,j;
/* first way*/
for(i=0;i<3;i++)
{
for(j=0;names[i][j]!='\0';j++)
printf("%c",names[i][j]);
printf("\n");
}
/* second way*/
for(i=0;i<3;i++)
{
for(j=0;j<strlen(names[i]);j++)
printf("%c",names[i][j]);
printf("\n");
}

/* third way*/
for(i=0;i<3;i++)
printf("%s\n",names[i]);

/* fourth way*/
for(i=0;i<3;i++)
puts(names[i]);

}

output:-
ASIA EUROPE INDIA
ASIA EUROPE INDIA
ASIA EUROPE INDIA
ASIA EUROPE INDIA

Run time initialization:-

#include<stdio.h>

main()
{
char names[10][20];
int i,j;
/* first way*/
for(i=0;i<3;i++)
for(j=0;j<10;j++)
scanf("%c",&names[i][j]);
for(i=0;i<3;i++)
puts(names[i]);

/* second way*/

for(i=0;i<3;i++)
scanf("%s",names[i]);
for(i=0;i<3;i++)
puts(names[i]);

/* third way*/
for(i=0;i<3;i++)
gets(names[i]);
for(i=0;i<3;i++)
puts(names[i]);
}

C Programming Lecture Notes
VVSV

118
Write a program to print number of times the given name is present in a double dimensional
character array.

#include<stdio.h>
main()
{
char names[10][20],temp[20];
int i,n,c=0;

printf("\nEnter no of names");
scanf("%d",&n);

for(i=0;i<n;i++)
gets(names[i]);

printf("\nThe names are:");
for(i=0;i<n;i++)
puts(names[i]);

printf("Enter the searching string:");
gets(temp);

for(i=0;i<n;i++)
if(strcmp(names[i],temp)==0)
c++;
if(c>0)
printf("No of times %s occured=%d",temp,c);
else
printf("%s is not found:",temp);
}




Output:-
Enter no of names:4
Anil
Anil
Sunil
Anil
The names are:
Anil
Anil
Sunil
Anil
Enter the searching string: Anil
No of times Anil occurred=3.


Enter no of names:4
Anil
Anil
Sunil
Anil
The names are:
Anil
Anil
Sunil
Anil
Enter the searching string: Kumar
Kumar is not found.

Double dimensional character arrays with functions:-
C Programming Lecture Notes
VVSV

119
Function prototype:-
Return-type function name(char array name[][size],int);
Calling function:-
Function name(array name,no of strings);
Called function:-
Function name(char array name[][size],int n)
{

}

Write a program to read and print the students names using functions.

#include<stdio.h>
void readnames(char [][20],int);
void displaynames(char [][20],int);
main()
{
char names[30][20];
int n;

printf("\nEnter no of names you want to enter\n");
scanf("%d",&n);

readnames(names,n);
displaynames(names,n);
}

void readnames(char names[][20],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Enter %d names",i+1);
scanf("%[^\n]",names[i]);
//gets(names[i]);
}
}
void displaynames(char names[][20],int n)
{
int i;
printf("\nThe names are:\n");
for(i=0;i<n;i++)
puts(names[i]);
}

Write a program to sort the given names alphabetically in ascending order.


#include<stdio.h>
void read(char [][20],int);
void display(char [][20],int);
void sort(char [][20],int);

main()
{
char names[30][20];
int n;

printf("\nEnter no of names you want to enter\n");
scanf("%d",&n);

read(names,n);
printf("Before sorting names are:");
C Programming Lecture Notes
VVSV

120

display(names,n);
sort(names,n);
printf("After sorting names are;");
display(names,n);
}

void read(char names[][20],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Enter %d names",i+1);
gets(names[i]);
}
}
void display(char names[][20],int n)
{
int i;
printf("\nThe names are:\n");
for(i=0;i<n;i++)
puts(names[i]);
}
void sort(char names[][20],int n)
{
char temp[20];
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(strcmp(names[j],names[j+1])>0)
{
strcpy(temp,names[j]);
strcpy(names[j],names[j+1]);
strcpy(names[j+1],temp);
}
}

Write a program to sort the given names alphabetically in ascending order according to their length..

if(strlen(names[j],names[j+1])==0)
{ (same as above)
strcpy(temp,names[j]);
strcpy(names[j],names[j+1]);
strcpy(names[j+1],temp);
}

Вам также может понравиться