Aditya University
FRESHMAN ENGINEERING DEPARTMENT
G. Sandhya Devi
Assistant Professor,
Freshmen Engineering Department
Extend a Hearty Welcome
to
Programming
for
Problem Solving Using C
Syllabus
2
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.
• In C programming, a string is a sequence of characters terminated with a
null character 0.
• A string can be declared using character array in c.
Declaration of strings:
• Declaring a string is as simple as declaring a one-dimensional array. Below
is the basic syntax for declaring a string.
syntax:
char string_name[size];
//string name is any name given to the string variable and size is used
to define the length of the string
example:
char mystring[20];
String
A string can be initialized in different ways.
Examples:
char str[] = “Aditya";
char str[50] = " Aditya";
char str[7] = {‘A’,’d',’i',’t',’y',’a’,'0'};
char str[] = {‘A’,’d',’i',’t',’y',’a’,'0'};
A d i t y a 0
Initializing a String
#include<stdio.h>
int main()
{
char str1[] = "Aditya";
char str2[7] = "Aditya";
char str3[7] = {'A','d','i','t','y','a','0'};
char str4[] = {'A','d','i','t','y','a','0'};
printf("%sn",str1);
printf("%s
n",str2);
printf("%sn",str3);
printf("%s
n",str4);
return 0;
}
Initializing a String
scanf can be used to read a string from keyboard
syntax:
scanf(“%s”,stringname);
#include<stdio.h>
int main ()
{
char str[20];
printf("Enter the string?");
scanf("%s",str);
printf("You entered %s",s);
return 0;
}
Execution:
Enter the string?Aditya
You entered Aditya
Enter the string?Aditya College
You entered Aditya
So,this scanf not reading entire string.it reads
upto first word
Reading a String
scanf to read string with spaces
syntax: scanf("%[^
n]s",stringname);
#include<stdio.h>
int main ()
{
char str[20];
printf("Enter the string?");
scanf("%[^n]s",str);
printf("You entered %s",str);
return 0;
}
Execution:
Enter the string?Aditya
You entered Aditya
Enter the string?Aditya College
You entered Aditya College
Reading a String
gets() to read string with spaces
syntax:
gets(stringname);
#include<stdio.h>
int main ()
{
char str[20];
printf("Enter the string?");
gets(str);
printf("You entered %s", str);
return 0;
}
fgets() to read string with spaces
syntax:
fgets(stringname,size,stdin); #include<stdio.h>
int main ()
{
char str[20];
printf("Enter the string?");
fgets(str,20,stdin)
printf("You entered %s",str);
return 0;}
o/p for both programs:
Enter the string?Aditya College
You entered Aditya College
Reading a String using gets(),fgets()
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.
3) strcat(first_string,
second_string)
concatinates or joins first string with second string.
The result of the string is stored in first string.
4) strcmp(first_string,
second_string)
compares the first string with second string. If both
strings are same, it returns 0.
5) strrev(string) returns reverse string.
6) strlwr(string) returns string characters in lowercase.
7) strupr(string) NrGeFtOuR PrRnO
BsL
E
MstSrOiL
VnI
NgG UcShI
N
GaCracters in uppercase
String handling functions
string functions defined in "string.h" library.
string operations using string handling functions
#include<stdio.h>
#include<string.h>
int main () {
char str1[20],str2[20],str3[20];
printf("nEnter the string1");
fgets(str1,20,stdin);
printf("n enter string2");
fgets(str2 ,20,stdin);
printf("length of string1 is %lu",strlen(str1));
strcat(str1,str2);
printf("n concatenation of string1,string2 is
%s ",str1);
strcpy(str3,str1);
printf("n string1 is %s",str1);
printf("n string2 is%s",str2);
printf("nstring3 is %s",str3);
printf("n comparison is
%d",strcmp(str2,str3));
return 0;
This function can be used to find a string’s length.
#include <stdio.h>
#include <string.h>
int main()
{
char a[20]="Program";
char b[20]={'P','r','o','g','r','a','m','0'};
printf("Length of string a = % n",strlen(a));
printf("Length of string b = %z n",strlen(b));
return 0;
}
Run Code
Output
Length of string a = 7
Length of string b = 7
Strlen( )
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
Strcpy( )
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) = %dn", result);
// comparing strings str1 and str3
result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %dn", result);
return 0;
}
Output
strcmp(str1, str2) = 1
strcmp(str1, str3) = 0
Strcmp( )
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: %sn", str1);
return 0;
}
Output:
The concatenated string is: Hello World
Strcat( )
Strupr()
It takes a string as input and converts all the
letters in the string to uppercase.
int main() {
char str[] = "Hello, World!";
printf("Original string: %sn", str);
strupr(str);
printf("Uppercase string: %sn", str);
return 0;
}
Output:
Original string: Hello, World!
Uppercase string: HELLO, WORLD!
Strlwr()
It takes a string as input and converts all the
letters in the string to lowercase.
int main()
{
char str[] = "Hello, World!";
printf("Original string: %sn", str);
// Convert string to lowercase
strlwr(str);
printf("Lowercase string: %sn", str);
return 0;
}
Output:
Original string: Hello, World!
Lowercase string: hello, world!
Strupr( ) & Strlwr()
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
Strrev( )
string operations without using string handling functions
length of string
#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++)
{
length ++;
}
printf("nLength of the String is : %d",length );
return 0;
}
Output:
Enter the String : aditya college
Length of the String is : 14
string copy
Output:
Enter the string :aditya college
Copied String is aditya college
#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];
i++; }
s2[i] = '0';
printf ("nCopied String is %s ", s2 );
return 0;
}
string concatenation
#include<stdio.h>
#include<string.h>
int main()
{
char s1[50], s2[30];
printf("nEnter String 1 :");
gets(s1);
printf("nEnter String 2 :");
gets(s2);
int i, j;
i = strlen(s1);
for (j = 0; s2[j] != '0'; i++, j++)
{
s1[i] = s2[j]; }
s1[i] = '0';
printf("nConcated string is :%s", s1);
return (0);
}
Output:
Enter String 1 :sandhya
Enter String 2 :devi
Concated string is :sandhyadevi
string comparison
#include<stdio.h>
#include<string.h>
main(){
char s1[50],s2[30];
int i,j,flag=0;
printf("nEnter String1:");
gets(s1);
printf("nEnter String2:");
gets(s2);
for(i=0,j=0;s1[i]!='0'&&s2[j]!='0';i++,j++)
{
if( s1 [ i ]!= s2 [ j ])
{
flag ++;
break;
}}
if(flag==0)
printf("nTwo strings are equals ");
else
printf("nTwo strings are not equal ");
}
Output:
Enter String1:welcome
Enter String2:welcome
Two strings are equals
21
string reverse
#include<stdio.h>
int main()
{
char s1[20];
int i,len;
printf("Reverse the string ");
printf("nEnter the string: ");
scanf(" %[^n]s",s1);
while(s1[i]!='0')
{
len=len+1;
i++ ;
}
for(i=len-1;i>=0;i--)
{
printf("%c",s1[i]);
}
return 0;
}
Output:
Reverse the string
Enter the string: aditya college'
'egelloc aytida

programming for problem solving using C-STRINGSc

  • 1.
    Aditya University FRESHMAN ENGINEERINGDEPARTMENT G. Sandhya Devi Assistant Professor, Freshmen Engineering Department Extend a Hearty Welcome to Programming for Problem Solving Using C
  • 2.
    Syllabus 2 UNIT – III Arraysindexing, 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.
  • 3.
    • In Cprogramming, a string is a sequence of characters terminated with a null character 0. • A string can be declared using character array in c. Declaration of strings: • Declaring a string is as simple as declaring a one-dimensional array. Below is the basic syntax for declaring a string. syntax: char string_name[size]; //string name is any name given to the string variable and size is used to define the length of the string example: char mystring[20]; String
  • 4.
    A string canbe initialized in different ways. Examples: char str[] = “Aditya"; char str[50] = " Aditya"; char str[7] = {‘A’,’d',’i',’t',’y',’a’,'0'}; char str[] = {‘A’,’d',’i',’t',’y',’a’,'0'}; A d i t y a 0 Initializing a String
  • 5.
    #include<stdio.h> int main() { char str1[]= "Aditya"; char str2[7] = "Aditya"; char str3[7] = {'A','d','i','t','y','a','0'}; char str4[] = {'A','d','i','t','y','a','0'}; printf("%sn",str1); printf("%s n",str2); printf("%sn",str3); printf("%s n",str4); return 0; } Initializing a String
  • 6.
    scanf can beused to read a string from keyboard syntax: scanf(“%s”,stringname); #include<stdio.h> int main () { char str[20]; printf("Enter the string?"); scanf("%s",str); printf("You entered %s",s); return 0; } Execution: Enter the string?Aditya You entered Aditya Enter the string?Aditya College You entered Aditya So,this scanf not reading entire string.it reads upto first word Reading a String
  • 7.
    scanf to readstring with spaces syntax: scanf("%[^ n]s",stringname); #include<stdio.h> int main () { char str[20]; printf("Enter the string?"); scanf("%[^n]s",str); printf("You entered %s",str); return 0; } Execution: Enter the string?Aditya You entered Aditya Enter the string?Aditya College You entered Aditya College Reading a String
  • 8.
    gets() to readstring with spaces syntax: gets(stringname); #include<stdio.h> int main () { char str[20]; printf("Enter the string?"); gets(str); printf("You entered %s", str); return 0; } fgets() to read string with spaces syntax: fgets(stringname,size,stdin); #include<stdio.h> int main () { char str[20]; printf("Enter the string?"); fgets(str,20,stdin) printf("You entered %s",str); return 0;} o/p for both programs: Enter the string?Aditya College You entered Aditya College Reading a String using gets(),fgets()
  • 9.
    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. 3) strcat(first_string, second_string) concatinates or joins first string with second string. The result of the string is stored in first string. 4) strcmp(first_string, second_string) compares the first string with second string. If both strings are same, it returns 0. 5) strrev(string) returns reverse string. 6) strlwr(string) returns string characters in lowercase. 7) strupr(string) NrGeFtOuR PrRnO BsL E MstSrOiL VnI NgG UcShI N GaCracters in uppercase String handling functions string functions defined in "string.h" library.
  • 10.
    string operations usingstring handling functions #include<stdio.h> #include<string.h> int main () { char str1[20],str2[20],str3[20]; printf("nEnter the string1"); fgets(str1,20,stdin); printf("n enter string2"); fgets(str2 ,20,stdin); printf("length of string1 is %lu",strlen(str1)); strcat(str1,str2); printf("n concatenation of string1,string2 is %s ",str1); strcpy(str3,str1); printf("n string1 is %s",str1); printf("n string2 is%s",str2); printf("nstring3 is %s",str3); printf("n comparison is %d",strcmp(str2,str3)); return 0;
  • 11.
    This function canbe used to find a string’s length. #include <stdio.h> #include <string.h> int main() { char a[20]="Program"; char b[20]={'P','r','o','g','r','a','m','0'}; printf("Length of string a = % n",strlen(a)); printf("Length of string b = %z n",strlen(b)); return 0; } Run Code Output Length of string a = 7 Length of string b = 7 Strlen( )
  • 12.
    The strcpy() functioncopies 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 Strcpy( )
  • 13.
    This function isused 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) = %dn", result); // comparing strings str1 and str3 result = strcmp(str1, str3); printf("strcmp(str1, str3) = %dn", result); return 0; } Output strcmp(str1, str2) = 1 strcmp(str1, str3) = 0 Strcmp( )
  • 14.
    Strcat() This function isused to concatenate two strings. int main() { char str1[] = "Hello"; char str2[] = "World"; strcat(str1, str2); printf("The concatenated string is: %sn", str1); return 0; } Output: The concatenated string is: Hello World Strcat( )
  • 15.
    Strupr() It takes astring as input and converts all the letters in the string to uppercase. int main() { char str[] = "Hello, World!"; printf("Original string: %sn", str); strupr(str); printf("Uppercase string: %sn", str); return 0; } Output: Original string: Hello, World! Uppercase string: HELLO, WORLD! Strlwr() It takes a string as input and converts all the letters in the string to lowercase. int main() { char str[] = "Hello, World!"; printf("Original string: %sn", str); // Convert string to lowercase strlwr(str); printf("Lowercase string: %sn", str); return 0; } Output: Original string: Hello, World! Lowercase string: hello, world! Strupr( ) & Strlwr()
  • 16.
    Strrev() It is usedto 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 Strrev( )
  • 17.
    string operations withoutusing string handling functions length of string #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++) { length ++; } printf("nLength of the String is : %d",length ); return 0; } Output: Enter the String : aditya college Length of the String is : 14
  • 18.
    string copy Output: Enter thestring :aditya college Copied String is aditya college #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]; i++; } s2[i] = '0'; printf ("nCopied String is %s ", s2 ); return 0; }
  • 19.
    string concatenation #include<stdio.h> #include<string.h> int main() { chars1[50], s2[30]; printf("nEnter String 1 :"); gets(s1); printf("nEnter String 2 :"); gets(s2); int i, j; i = strlen(s1); for (j = 0; s2[j] != '0'; i++, j++) { s1[i] = s2[j]; } s1[i] = '0'; printf("nConcated string is :%s", s1); return (0); } Output: Enter String 1 :sandhya Enter String 2 :devi Concated string is :sandhyadevi
  • 20.
    string comparison #include<stdio.h> #include<string.h> main(){ char s1[50],s2[30]; inti,j,flag=0; printf("nEnter String1:"); gets(s1); printf("nEnter String2:"); gets(s2); for(i=0,j=0;s1[i]!='0'&&s2[j]!='0';i++,j++) { if( s1 [ i ]!= s2 [ j ]) { flag ++; break; }} if(flag==0) printf("nTwo strings are equals "); else printf("nTwo strings are not equal "); } Output: Enter String1:welcome Enter String2:welcome Two strings are equals
  • 21.
    21 string reverse #include<stdio.h> int main() { chars1[20]; int i,len; printf("Reverse the string "); printf("nEnter the string: "); scanf(" %[^n]s",s1); while(s1[i]!='0') { len=len+1; i++ ; } for(i=len-1;i>=0;i--) { printf("%c",s1[i]); } return 0; } Output: Reverse the string Enter the string: aditya college' 'egelloc aytida