ppsc unit3 strings-1 by anesu chirozva
ppsc unit3 strings-1 by anesu chirozva
Programming
for
Problem Solving Using C
G. Sandhya Devi
Assistant Professor,
Freshmen Engineering Department
Syllabus
UNIT – III
Arrays indexing, Accessing programs with array of integers, two dimensional arrays,
Introduction to Strings, string handling functions.
Sorting techniques: bubble sort, selection sort.
Searching Techniques: linear , Binary search.
2
String
#include<stdio.h>
printf("%s\n",str1);
int main()
printf("%s\
{
n",str2);
char str1[] = "Aditya";
printf("%s\n",str3);
printf("%s\
char str2[7] = "Aditya";
return 0;
n",str4);
}
char str3[7] = {'A','d','i','t','y','a','\0'};
#include<stdio.h>
gets() to read string with spaces int main ()
{
syntax: char str[20];
gets(stringname); printf("Enter the string?");
gets(str);
printf("You entered %s", str);
fgets() to read string with spaces return 0;
}
syntax: o/p for both programs:
Enter the string?Aditya College
fgets(stringname,size,stdin); #include<stdio.h> You entered Aditya College
int main ()
{
char str[20];
printf("Enter the string?");
fgets(str,20,stdin)
printf("You entered %s",str);
return 0;}
String handling functions
string functions defined in "string.h" library.
No. Function Description
1) strlen(string_name) returns the length of string name.
2) strcpy(destination, source) copies the contents of source string to destination
string.
return 0;
}
Run Code
Output
Length of string a = 7
Length of string b = 7
Strcpy( )
The strcpy() function copies the string pointed by source (including the null
character) to the destination.
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = “good";
char str2[20];
// copying str1 to str2
strcpy(str2, str1);
puts(str2);
return 0;
}
Output
good
Strcmp( )
This function is used to compare two strings.
Example:
int main() {
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;
// comparing strings str1 and str2
result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result);
// comparing strings str1 and str3
result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %d\n", result);
return 0;
}
Output
strcmp(str1, str2) = 1
strcmp(str1, str3) = 0
Strcat( )
Strcat()
This function is used to concatenate two strings.
int main()
{
char str1[] = "Hello";
char str2[] = "World";
strcat(str1, str2);
printf("The concatenated string is: %s\n", str1);
return 0;
}
Output:
The concatenated string is: Hello World
Strupr( ) & Strlwr()
Strupr() Strlwr()
It takes a string as input and converts all the
It takes a string as input and converts all the
letters in the string to lowercase.
letters in the string to uppercase.
int main()
int main() {
{
char str[] = "Hello, World!";
char str[] = "Hello, World!";
printf("Original string: %s\n", str);
printf("Original string: %s\n", str);
strupr(str);
// Convert string to lowercase
printf("Uppercase string: %s\n", str);
strlwr(str);
return 0;
printf("Lowercase string: %s\n", str);
}
return 0;
Output:
}
Original string: Hello, World!
Output:
Uppercase string: HELLO, WORLD!
Original string: Hello, World!
Lowercase string: hello, world!
Strrev( )
Strrev()
It is used to reverse a given string. It takes a string as an argument and returns a
pointer to the reversed string.
int main()
{
char str[40]; // declare the size of character string
printf(" \n Enter a string to be reversed: ");
scanf("%s", str);
printf(" \n After the reverse of a string: %s ", strrev(str));
return 0;
}
Output:
Enter a string to be reversed: apple
After the reverse of a string: elppa
length of
string operationsstring
without using string handling functions
#include<stdio.h>
int main()
{
char str [100];
int length, i ;
printf ("\nEnter the String : ");
gets(str);
length=0;
for(i=0;str[i]!='\0';i++)
{ Output:
length ++; Enter the String : aditya college
Length of the String is : 14
}
printf("\nLength of the String is : %d",length );
return 0;
}
string copy
#include<stdio.h>
int main()
{
char s1 [100] , s2 [100]; int i=0;
printf ("\nEnter the string :");
gets ( s1 );
while(s1[i]!= '\0')
{
s2[i] = s1[i]; Output:
Enter the string :aditya college
i++; } Copied String is aditya college
s2[i] = '\0';
printf ("\nCopied String is %s ", s2 );
return 0;
}
string concatenation
#include<stdio.h> for(i=len-1;i>=0;i--)
int main() {
{ printf("%c",s1[i]);
char s1[20]; }
int i,len; return 0;
printf("Reverse the string "); }
printf("\nEnter the string: ");
scanf(" %[^\n]s",s1);
Output:
while(s1[i]!='\0') Reverse the string
{ Enter the string: aditya college'
len=len+1; 'egelloc aytida
i++ ;
}
21