0% found this document useful (0 votes)
11 views31 pages

Strings

The document provides an overview of character arrays and strings in C programming, including their declaration, initialization, and various operations such as reading, writing, and manipulating strings. It covers essential string handling functions from the string.h library, as well as examples of string operations without using library functions. Additionally, it discusses sorting and searching methods for arrays.

Uploaded by

adityavarpe69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views31 pages

Strings

The document provides an overview of character arrays and strings in C programming, including their declaration, initialization, and various operations such as reading, writing, and manipulating strings. It covers essential string handling functions from the string.h library, as well as examples of string operations without using library functions. Additionally, it discusses sorting and searching methods for arrays.

Uploaded by

adityavarpe69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Character Arrays and 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];

• Ex. Char str[20];


C String Initialization
• char str[] = “AISSMSCOE";

• char str[50] = “AISSMSCOE";

• char str[10] = { ‘A',‘I',‘S',‘S',‘M',‘S',‘C',‘O',‘E','\0'};

• char str[] = { ‘A',‘I',‘S',‘S',‘M',‘S',‘C',‘O',‘E','\0'};


Reading string as an input
• Char str[30];
• scanf(“%s”,&str); //stops scanning if whitespace occurs

Or

• gets(str);//dangerous as it puts no limit on the input, so can crash the


system

Or

• fgets(str, sizeof(str), stdin);//Okay to use than above two


Displaying the string
• Char str[30];

• 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);

• Copies source string to destination string.


Using strcpy()
char str1[20],str2[20]; • char str1[20] = "Hello World!";
gets(str1); char str2[20];

// Copy str1 to str2


Strcpy(str2,str1); strcpy(str2, str1);

// 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];

// Copy str1 to str2


Strncpy(str2,str1,4); strncpy(str2, str1,4);

// 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 value==0,both strings are equal // Compare str1 and str3, and


If value!=0,strings are not equal print the result
printf("%d\n", strcmp(str1,
str3)); // Returns non zero value
(the strings are not equal)
char s1[] = “Hello World";
char s2[] = “or";
Strstr() char* result;
• function returns a pointer to result = strstr(s1, s2);
the position of the first
occurrence of a string in if (result != NULL) {
another string. // If 'result' is not NULL, it means the substring
// was found, so print it
printf("Substring found: %s\n", result);
Syntax: }
Strstr(main_string,substring); else {
// If 'result' is NULL, it means the substring was
// not found, so print appropriate message
printf("Substring not found.\n");
}
Example:
#include <stdio.h>
#include <string.h>
int main() {
char sentence[] = "This is a sample
sentence.";
if (strstr(sentence,’is’)!=NULL)
printf(“Substring is present in string);
else
printf(“Substring is not present in string”);
}
Example:
#include <stdio.h>
strchr() #include <string.h>
int main() {
• This function locates the first char sentence[] = "This is a sample
occurrence of a character in a sentence.";
string.
char *ptr = strchr(sentence, 'a');
if(ptr!=NULL)
printf(“Character Present”);
else
printf(“character is not in string”);
return 0;
}
Output:
Character Present
Ex.
#include <stdio.h>
#include <string.h>
int main() {
char sentence[] = "This is a sample sentence.";
char *ptr = strchr(sentence, 'a');
printf("strchr(): %s\n", ptr);
return 0;
}
Output:
strchr(): a sample sentence.
Example:
#include <stdio.h>
#include <string.h>
int main() {
char sentence[] = "This is a sample
sentence.";
if (strchr(sentence,’a’)!=NULL)
printf(“character is present in string);
else
printf(“character is not present in string”);
}
Example:
strrchr() #include <stdio.h>
#include <string.h>
• This function locates the last int main() {
occurrence of a character in a char sentence[] = "This is a sample
string. sentence.";
char *lastPtr = strrchr(sentence, 'a');
printf("strrchr(): %s\n", lastPtr);
return 0;
}
Output:
strrchr(): ample sentence.
Example:
#include <stdio.h>
#include <string.h>
int main() {
char sentence[] = "This is a sample sentence.";
if (strrchr(sentence,’a’)!=NULL)
printf(“character is present in string);
else
printf(“character is not present in string”);
}
String Operations without C library functions
Determine Length of string
#include <stdio.h>
int main()
{
char str[100];
int i,length=0;

printf("Enter a string: \n");


scanf("%s",str);
Output:
for(i=0; str[i]!='\0'; i++) Enter a string: AISSMS
{ Length of input string: 6
length++;
}
printf("\nLength of input string: %d",length);
return 0;
}
String Copy
#include <stdio.h>
int main() {
char s1[100], s2[100], i;
printf("Enter string s1: ");
fgets(s1, sizeof(s1), stdin);
Output:
for (i = 0; s1[i] != '\0'; ++i) {
Enter string s1: Hey fellow
s2[i] = s1[i]; programmer.
}
String s2: Hey fellow programmer.
s2[i] = '\0';
printf("String s2: %s", s2);
return 0;
}
String compare
/* without using strcmp() */ else
{
#include <stdio.h>
flag=1;
#include <string.h> break;
int main() }

{
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.

You might also like