String Handling on
String Handling on
String - group of chars. for implement/use string in C & C++, we have concept
of char array.
** means C & C++ ke inside string store karne ke liye alag se koi data type
nahi, for that we need to use char array. but in java we have String class for
store the String,
char nm[10]; - we can store 10 chars string. - means we can store Max 10 chars
string. 9 chars + 1 is the string terminator char / null char &
that is represent with - '\0'
Q- null char memory leta hai ya nahi? - ans - always says lete hai.
Q- we have char ar[10] - how many char string we can store inside ar array?
Ans - 9 char string + 1 char/index reserved for null char , null char
represent '\0' or we can use 0 also
ar[0] = 'G' ;
ar[1] = 'a' ;
ar[2] = 'u' ;
ar[3] = 'r' ;
ar[4] = 'a' ;
ar[5] = 'v' ;
ar[6] = '\0' ; - NULL char, string terminator char.
NULL char Major use for work with String - means NULL char String handling me
major role play karta hai.
C String Handling & File Handling Notes - on 23-01-2025 by Manoj Rajora Page 1
nm = "Gaurav"; - wrong way, we can't initialize String with = operator , for
that we need to strcpy() function.
for string handling in C & C++ we have string.h header file, that contain the
functions for string handling.
** all string handling functions prefix with str word - strcat / strrev /
strcpy / strlen / strupr / strlwr / strcmp / strcmpi / strncmp / strncmpi()
- Total we have 10 functions
6 functions for normal operations. - strcpy() , strcat(), strupr(), strlwr(),
strlen(), strrev().
4 functions for String Compare - strcmp / strcmpi / strncmp / strncmpi()
char nm[15];
nm = "Gaurav"; - wrong way in C & C++.
strcpy(nm, "Gaurav"); now nm is = "Gaurav"
char arr[10];
char ar[] = "Gaurav"; - declare time.
gets(ar); / scanf("%s",ar); - for run time input.
ar = "Gaurav";
ar = "Gaurav";
int ctr = 0;
while(ar[ctr] != '\0') // while not end of the String.
{
arr[ctr] = ar[ctr]; // copy char by char in arr from ar index.
ctr ++;
}
arr[ctr] = '\0';
C String Handling & File Handling Notes - on 23-01-2025 by Manoj Rajora Page 2
2nd - strcat(str1,str2) - for String concat
int a = 10; - NOW WE WANT TO ADD 5 VALUE IN a, a = a + 5; / a += 5; NOW a = 15
int x;
clrscr();
x = 0;
while(ar[x]!='\0') // while not end of string,means jab tak null char na ho
{
if(ar[x]>='a' && ar[x]<='z') // means small a to z hai to
ar[x] = ar[x]-32;
x++;
}
printf("\nString in upper case %s",ar);
// program for convert string into upper case with strupr() function & without strupr()
function - with own algo
void main()
{
char ar[] = "Gaurav Kumar";
int x;
clrscr();
/*
strupr(ar);
printf("\nString in upper case %s",ar);
*/
x = 0;
while(ar[x]!='\0')
{
if(ar[x]>='a' && ar[x]<='z')
ar[x] = ar[x]-32;
x++;
}
printf("\nString in upper case %s",ar);
getch();
}
C String Handling & File Handling Notes - on 23-01-2025 by Manoj Rajora Page 3
4th - strlwr(str); - to convert string into lower case
ASCII code - A - 65 to Z - 90
a - 97 to z - 122
-----------------
32 32
int x;
clrscr();
x = 0;
while(ar[x]!='\0')
{
if(ar[x]>='A' && ar[x]<='Z')
ar[x] = ar[x]+32;
x++;
}
printf("\nString in Lower case %s",ar);
C String Handling & File Handling Notes - on 23-01-2025 by Manoj Rajora Page 4
String reverse without any function & store in a variable
ctr = 0;
while(ar[ctr]!='\0') // loop for count String length.
ctr ++;
ar = Babu
i = 0 j = 4
ar[0] = 'B'; ctr = 1 arr[i=0] = ar[j = 3]= 'u'
ar[1] = 'a'; ctr = 2 arr[i=1] = ar[j = 2]= 'b'
ar[2] = 'b'; ctr = 3 arr[i=2] = ar[j = 1]= 'a'
ar[3] = 'u'; ctr = 4 arr[i=3] = ar[j = 0]= 'B'
ar[4] = '\0'; ctr = 5 arr[i=4] = '\0';
Dry Run -
C String Handling & File Handling Notes - on 23-01-2025 by Manoj Rajora Page 5
malayalam
012345678
len = 9;
compare -
str[i=0] = str[(len=9)-(i=0)-1 = 8] compare - m = m
str[i=1] = str[(len=9)-(i=1)-1 = 7] compare - a = a
str[i=2] = str[(len=9)-(i=2)-1 = 6] compare - l = l
str[i=3] = str[(len=9)-(i=3)-1 = 5] compare - a = a
str[i=4] = str[(len=9)-(i=4)-1 = 4] compare - y = y
** compare with char by char(with ASCII code) & return the diff of ASCII code.
ASCII code - A - 65 to Z - 90
a - 97 to z - 122
a = "Ankit"; b = "Chintu";
if(strcmp(a,b)>0) - -2 - condition false
if(strcmp(b,a)>0) - +2 - condition true
8th - int strcmpi(str1,str2) - compare 2 Strings with ignore case - means case
in-sensitive.
** compare with char by char(with ASCII code) & return the diff of ASCII code.
C String Handling & File Handling Notes - on 23-01-2025 by Manoj Rajora Page 6
a = "ankit"; b = "ANKIT";
if(strcmpi(a,b)==0) - 0 - true
a = "ANKIT", b "Chintu";
if(strcmpi(a,b)<0) - -2 - condition true
* it internally convert into one case - may be upper or lower then compare it
char by char
#include<stdio.h>
#include<conio.h>
#define MAX 10
void main()
{
char tmp[20],nm[MAX][20]; //10 rows- 10names, each name contain max 20 chars
int i,j;
clrscr();
for(i=0;i<MAX;i++)
{
printf("Plz Enter name %d : ",i+1);
gets(nm[i]);
strupr(nm[i]);
}
// for sorting - selection sort.
for(i=0;i<MAX-1;i++)
{
for(j=i+1;j<MAX;j++)
{
if(strcmp(nm[i],nm[j])>0) // swap 2 strings with 3rd var.
{
strcpy(tmp,nm[i]);
strcpy(nm[i],nm[j]);
strcpy(nm[j],tmp);
}
}
}
// for display
for(i=0;i<MAX;i++)
printf("\nWelcome %s",nm[i]);
getch();
}
C String Handling & File Handling Notes - on 23-01-2025 by Manoj Rajora Page 7
9th - int strncmp(str1,str2,noc) - compare 2 Strings with no of chars -
without ignore case - means case sensitive.
it return - 0 - if both strings r equals
+ve - if 1st string is greater
-ve - if 1st string is small.
** compare with char by char(with ASCII code) & return the diff of ASCII code.
a = "Amandeep";
b = "Aman Singh";
if(strncmp(a,b,4)==0) {} - compare starting 4 chars - true
a = "AMANDEEP";
b = "Aman Singh";
if(strncmpi(a,b,4)==0) {} true - compare starting 4 chars with ignore case
// compare 2 Strings without any function - with own algo - using for loop
C String Handling & File Handling Notes - on 23-01-2025 by Manoj Rajora Page 8
FILE I/O
Why file handling is needed ?
There are many functions in the C library to open, read, write, search and
close the file. A list of file functions are given below:
if we want to read / write data from / in a file for that we can use
file handling.
FILE *f;
C String Handling & File Handling Notes - on 23-01-2025 by Manoj Rajora Page 10
2nd - now open an file in input or output mode with - fopen() function.
FILE* fopen(filename,mode);
mode - r - read mode - open an file in input mode
r+ - read & write mode - 1st read then write
w - write mode - open an file in output mode
w+ - write & read - 1st write then we can read
a - append mode - open an file in append mode (exiting + new data)
a+ - append & read - 1st append then read
b - binary mode - open an file in binary mode
x - if file already exist then not create.
f = fopen("c:/F1.c","r");
feof(file) - file end of the file - return true if file end of the file.
C String Handling & File Handling Notes - on 23-01-2025 by Manoj Rajora Page 11
// to read own source code & display on console window.
#include<stdio.h>
void main()
{
FILE *r;
char c;
clrscr();
r = fopen("F1.c","rb"); // open F1 file in input + binary mode
if(r==NULL)
printf("Sorry ur file not found for input...");
else {
while(1)
{
c = fgetc(r);
if(feof(r)) break;
delay(50); // 1000ms = 1 sec
printf("%c",c);
}
fclose(r);
}
getch();
}
// to read own source code & display on console + write in a file also
#include<stdio.h>
void main()
{
FILE *r,*w;
char c;
clrscr();
r = fopen("F1.c","rb"); // open F1 file in input + binary mode
w = fopen("gaurav.txt","wb"); // open aman.txt file in output + binary mode
if(r==NULL)
printf("Sorry ur file not found for input...");
else {
while(1)
{
c = fgetc(r);
if(feof(r)) break;
fputc(c,w); // write char in w file
delay(50); // 1000ms = 1 sec
printf("%c",c);
}
fclose(r);
fclose(w);
}
getch();
}
C String Handling & File Handling Notes - on 23-01-2025 by Manoj Rajora Page 12
// read & write data line by line
#include<stdio.h>
void main()
{
FILE *r,*w;
char ar[80];
clrscr();
r = fopen("F1.c","rb"); // open F1 file in input + binary mode
w = fopen("gaurav.txt","wb"); // open aman.txt file in output + binary mode
if(r==NULL)
printf("Sorry ur file not found for input...");
else {
while(1) {
fgets(ar,80,r); // read 80 char from r file & stored inside ar array
if(feof(r)) break;
fputs(ar,w); // write char in w file
delay(50); // 1000ms = 1 sec
printf("%s",ar);
}
fclose(r);
fclose(w);
}
getch();
}
C String Handling & File Handling Notes - on 23-01-2025 by Manoj Rajora Page 13