Strings
Strings
-by
Ujwala H. Wanaskar
Contents:
• Character Arrays and Strings:
• Declaration and Initialization String Variables
• Reading Strings from Terminal
• Writing Strings to Screen
• Putting Strings Together
• Comparison of Two Strings
• Introduction to String handling Functions
Definition:
• A String in C programming is a sequence of characters terminated
with a null character ‘\0’.
• The C String is stored as an array of characters.
• The difference between a character array and a C string is that the
string in C is terminated with a unique character ‘\0’.
Declaration Syntax
• char string_name[size];
Or
Or
• Printf(“%s”,str);
Or
puts(str);
Or
fputs(str,stdout);
String Library function(string.h)
1. Strlen()
2. Strcpy()
3. Strncpy()
4. Strcat()
5. Strncat()
6. Strcmp()
1.strlen():finds length of a string
char str[20];
int len;
gets(str);
len=strlen(str);
Or
printf(“Length=%d”,strlen(str));
2.strcpy:copying the string
• Syntax:
• Strcpy(destination_string,source_string);
// Print str2
Or printf("%s", str2);
Strcpy(str2,”Hello”);
strncpy()
• Copies first n characters from source string to destination string.
• Strncpy(dest_string,source_string,n);
Using strncpy()
char str1[20],str2[20]; • char str1[20] = "Hello World!";
gets(str1); char str2[20];
// Print str2
printf("%s", str2);
strcat()
• to concatenate (combine) two • char str1[20] = "Hello ";
strings, we use the strcat() char str2[] = "World!";
function:
// Concatenate str2 to str1
(result is stored in str1)
• strcat(str2,str1); strcat(str1, str2);
// Print str1
printf("%s", str1);
strncat()
• to concatenate (combine) two • char str1[20] = "Hello ";
strings, we use the strncat() char str2[] = "World!";
function:
// Concatenate str2 to str1
(result is stored in str1)
• strcat(str2,str1,n); strncat(str1, str2,2);
// Print str1
printf("%s", str1);
• char str1[] = "Hello";
strcmp() char str2[] = "Hello";
char str3[] = "Hi";
• To compare two strings
// Compare str1 and str2, and
Syntax: print the result
printf("%d\n", strcmp(str1,
Value=strcmp(str1,str2); str2)); // Returns 0 (the strings
are equal)
{
If(flag==1 ||str1[i]!=str2[i])
char Str1[100], Str2[100];
printf(“strings are not equal”);
int result, I,flag=0; else
printf("\n Please Enter the First String : "); printf(“Strings are equal”);
Return 0;
gets(Str1);
}
printf("\n Please Enter the Second String : ");
gets(Str2); O/P:
Please Enter the First String : cat
for(i = 0; Str1[i] != '\0'; i++)
{ Please Enter the Second String : dog
if(str1[i]==str2[i])
continue; strings are not equal
}
String concatenation
#include <stdio.h> str1[i]='\0';
int main() printf("\nOutput: %s",str1);
{ return 0;
char str1[50], str2[50], i, j; }
printf("\nEnter first string: ");
scanf("%s",str1);
printf("\nEnter second string: "); • O/P:
scanf("%s",str2); • Enter first string:hello
for(i=0; str1[i]!='\0'; ++i); • Enter second string:World
• Output:helloWorld
for(j=0; str2[j]!='\0'; ++j, ++i)
{
str1[i]=str2[j];
}
String Reverse
int main() {
char str[100] = "Hello World"; • O/P:
char rstr[100];
i=len-1;
j=0; • Original String: Hello World
while(i>=0) • Reversed string: dlroW olleH
{
rstr[j]=str[i];
i--;
j++;
}
rstr[j]='\0';
printf("\n\n Original string=%s",str);
printf("\n\n Reveresed string =%s",rstr);
return 0;
}
Array Sorting methods:
1. Bubble Sort
2. Selection sort
3. Insertion Sort.
4. Heap sort
5. QuickSort
6. Merge Sort
7. Shell sort
Array Searching Methods
1. Sequential Search/Linear Search
2. Binary search.