0% found this document useful (0 votes)
36 views

2 - String

Uploaded by

fishdish001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

2 - String

Uploaded by

fishdish001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

VIDYA PRATISHTHAN’S COMMERCE & SCIENCE COLLEGE INDAPUR.

Chapter No: 2
Chapter Name: Strings

String:
 A String in C is nothing but a collection of characters.
 In C, Strings is represented by arrays of characters.
 Strings are always ended with null character (‘\0’).
 Strings are always enclosed by double quotes. Whereas, character is enclosed by
single quotes in C.
 When we define a string as char s[10], the character s[10] is implicitly initialized
with the null in the memory.

Syntax:
char s[5];
or
char s[]=”VPCSC”;
or
char s[]={‘V’,’P’,’C’,’S’,’C’,’\0’};

V P C S C \0

Example :

Input Output
#include <stdio.h>
void main() Enter String : VPCSC
{ String = VPCSC
char s[10];
printf("Enter String : ");
gets(s);
printf("String = %s",s);
}

ADVANCED C (FYBCS) BY PROF. SARFARAZ SHAIKH 1


VIDYA PRATISHTHAN’S COMMERCE & SCIENCE COLLEGE INDAPUR.

String Functions:

No. Function Description

1) strlen(string_name) returns the length of string name.

2) strcpy(destination, copies the contents of source string to


source) destination string.

3) strcat(first_string, concats or joins first string with second string.


second_string) The result of the string is stored in first string.

4) strcmp(first_string, compares the first string with second string. If


second_string) both strings are same, it returns 0.

5) strrev(string) returns reverse string.

6) strlwr(string) returns string characters in lowercase.

7) strupr(string) returns string characters in uppercase.

8) strstr(str1,str2) Returns pointer to first occurrence of str2 in str1

9) strtok( ) Tokenizing given string using delimiter

10) strset( ) Sets all character in a string to given character

ADVANCED C (FYBCS) BY PROF. SARFARAZ SHAIKH 2


VIDYA PRATISHTHAN’S COMMERCE & SCIENCE COLLEGE INDAPUR.

Input Output
1. strlen

#include <stdio.h> Enter String : vpcsc


#include <string.h>
Length of String = 5
void main()
{
char s[10];
printf("Enter String : ");
gets(s);
printf("Length of String = %d",strlen(s));
}

2. strrev
#include <stdio.h>
#include <string.h> Enter String : vpcsc

void main()
Reverse of String = cscpv
{
char s[10];
printf("Enter String : ");
gets(s);
printf("Reverse of String = %s",strrev(s));
}
3. strlwr()

#include <stdio.h> Enter String : VPCSC


#include <string.h>
void main() String in Lowercase = vpcsc
{
char s[10];
printf("Enter String : ");
gets(s);
printf("String in Lowercase = %s",strlwr(s));
}

ADVANCED C (FYBCS) BY PROF. SARFARAZ SHAIKH 3


VIDYA PRATISHTHAN’S COMMERCE & SCIENCE COLLEGE INDAPUR.

4. strupr()

#include <stdio.h> Enter String : indapur


#include <string.h>
String in Uppercase = INDAPUR
void main()
{
char s[10];
printf("Enter String : ");
gets(s);
printf("String in Uppercase = %s",strupr(s));
}
5. strcpy()

#include <stdio.h> Enter String : vpcsc


#include <string.h>
S1 = vpcsc
void main()
{ S2 = vpcsc

char s1[10],s2[10];
printf("Enter String : ");
gets(s2);
strcpy(s1,s2);
printf("S1 = %s\nS2 = %s",s1,s2);
}
6. strcat()

#include <stdio.h> Enter First String : vp


#include <string.h>
Enter Second String : Indapur
void main()
{ char s1[10],s2[10]; S1 = vpIndapur

printf("Enter First String : "); S2 = Indapur


gets(s1);
printf("Enter Second String : ");
gets(s2);
strcat(s1,s2);
printf("S1 = %s\nS2 = %s",s1,s2);
}

ADVANCED C (FYBCS) BY PROF. SARFARAZ SHAIKH 4


VIDYA PRATISHTHAN’S COMMERCE & SCIENCE COLLEGE INDAPUR.

7. strcmp()

#include <stdio.h> Enter First String : vpcsc


#include <string.h>
Enter Second String : vpcsc
void main()
{ Both Strings are Equal

char s1[10],s2[10];
printf("Enter First String : ");
gets(s1);
printf("Enter Second String : ");
gets(s2);
if((strcmp(s1,s2))==0)
printf("Both Strings are Equal");
else
printf("Both Strings are Not Equal");
}

String Programs

Input Output

Q . Write a C Program to print alternate character from given string

Enter String : vpcsc


#include<stdio.h>
#include<string.h> vcc
void main()
{
char s[10];
int i;
printf("Enter String : ");
gets(s);
for(i=0;i<strlen(s);i+=2)
printf("%c",s[i]);
}

ADVANCED C (FYBCS) BY PROF. SARFARAZ SHAIKH 5


VIDYA PRATISHTHAN’S COMMERCE & SCIENCE COLLEGE INDAPUR.

Q. Write a C Program to reverse a given string without using standard function.

Enter String : vpcsc


#include<stdio.h>
#include<string.h> cscpv
void main()
{
char s[10];
int i;
printf("Enter String : ");
gets(s);
for(i=strlen(s)-1;i>=0;i--)
printf("%c",s[i]);
}

Q. Write a C Program to print vowels from given string.

Enter String : indapur


#include<stdio.h>
#include<string.h> iau
void main()
{
char s[10];
int i;
printf("Enter String : ");
gets(s);
for(i=0;i<strlen(s);i++)
{
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u')
printf("%c",s[i]);
}
}

ADVANCED C (FYBCS) BY PROF. SARFARAZ SHAIKH 6


VIDYA PRATISHTHAN’S COMMERCE & SCIENCE COLLEGE INDAPUR.

Q. Write a C Program to count and print vowels from a given string.

#include<stdio.h>
Enter String : indapur
#include<string.h>
void main() iau
{
Total Vowels = 3
char s[10];
int i,cnt=0;
printf("Enter String : ");
gets(s);
for(i=0;i<strlen(s);i++)
{
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u')
{
cnt++;
printf("%c",s[i]);
}
}
printf("\nTotal Vowels = %d",cnt);
}
Q. Write a C Program to print string pattern.

#include<stdio.h>
#include<string.h> Enter String : vpcsc
void main()
v
{
v p
char s[10];
v p c
int i,j;
v p c s
printf("Enter String : "); v p c s c
gets(s);
for(i=0;i<strlen(s);i++)
{
for(j=0;j<=i;j++)
printf(" %c ",s[j]);
printf("\n");
}
}

ADVANCED C (FYBCS) BY PROF. SARFARAZ SHAIKH 7


VIDYA PRATISHTHAN’S COMMERCE & SCIENCE COLLEGE INDAPUR.

Q. Write a C Program to accept multiple strings and print it.

#include<stdio.h>
#include<string.h> Enter Five Strings :
#include<conio.h> VPCSC
void main() TC
{ SP
char s[5][10]; VPP
int i; JSPM
clrscr(); Accepted Strings :
printf("Enter Five Strings : \n"); VPCSC
for(i=0;i<5;i++) TC
gets(s[i]); SP
printf("Accepted Strings : \n"); VPP
for(i=0;i<5;i++) JSPM
puts(s[i]);
}

ADVANCED C (FYBCS) BY PROF. SARFARAZ SHAIKH 8

You might also like