Strings

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

<Strings>

Strings :
• String is a set of characters.
• It is one dimensional array type of char.
• Every string terminated by null character ‘\0’ whose integer value
equivalent to zero.

Different ways to initialize strings:

char a[ ]=“LogikWorks”; (or)


char a[ ]={‘L’,’o’,’g’,’i’,’k’,’w’,’o’,’r’,’k’,’s’};
char a[5][10]; (or)
char a[5][10]={“raju”,”ravi”,”arun”,”kumar”};
Taking input from user:
char name[10];
scanf("%s", name);
Sample code on String:
#include <stdio.h>
int main()
{
char name[20], ch; // declaration of a character array
int i = 0;
printf("Enter a string:\n"); // taking string input from the user
while (ch != '\n’)
{ch = getchar();
name[i] = ch;
i++; }
name[i] = '\0'; // ending the string
i = 0; // display the string
printf("The string is:\n");
while (name[i] != '\0') {
printf("%c", name[i]);
i++; }
return 0;
}
Two dimensional character arrays
 2D character arrays are very similar to 2D integer arrays

 A 2D character array is more like a String array.

 It allows us to store multiple strings under the same name.

Declaration and Initialization a 2D character array


char name[5][10]

char name[5][10] = {
"tree",
"bowl",
"hat",
"mice",
"toon"
};
Program to search for a string in the string array
/*process for finding the item in the string array*/
#include <stdio.h> for (i = 0; i < 5; i++) {
#include <string.h> x = strcmp(&name[i][0],
int main() { item); // compares the string in the array with
char name[5][10], the item and if
item[10]; // declaring the string array and the character // match is found returns 0 and stores it in
// array that will contain the string to be matched variable x
if (x == 0)
int i, x, f = 0; f = i;
}
/*taking 5 string inputs*/
printf("Enter 5 strings:\n"); /*if match is not found*/
for (i = 0; i < 5; i++) if (f == 0)
scanf("%s", &name[i][0]); printf("the item does not match any name in the list");
/*If the match is found*/
else
/*entering the item to be found in the string array*/ printf("Item Found. Item in the array exists at index -
printf("Enter the string to be searched:\n"); %d", f);
scanf("%s", &item); return 0;
}
String handling Functions:
strcpy() strcpy(string1, string2) Copies string2 value into string1
strncpy() strncpy(string1, string2, 5) Copies first 5 characters string2 into string1

strlen() strlen(string1) returns total number of characters in string1

strcat() strcat(string1,string2) Appends string2 to string1


strncat() strncpy(string1, string2, 4) Appends first 4 characters of string2 to string1

strcmp() strcmp(string1, string2) Returns 0 if string1 and string2 are the same;
less than 0 if string1<string2; greater than 0 if
string1>string2

strncmp() strncmp(string1, string2, 4) Compares first 4 characters of both string1 and


string2
strcmpi() strcmpi(string1,string2) Compares two strings, string1 and string2 by
ignoring case (upper or lower)
stricmp() stricmp(string1, string2) Compares two strings, string1 and string2 by
ignoring case (similar to strcmpi())

strlwr() strlwr(string1) Converts all the characters of string1 to lower


case.
strupr() strupr(string1) Converts all the characters of string1 to upper
case.
String handling functions:
strdup() string1 = strdup(string2) Duplicated value of string2 is assigned to string1

strchr() strchr(string1, 'b') Returns a pointer to the first occurrence of


character 'b' in string1

strrchr() 'strrchr(string1, 'b') Returns a pointer to the last occurrence of


character 'b' in string1

strstr() strstr(string1, string2) Returns a pointer to the first occurrence of


string2 in string1

strset() strset(string1, 'B') Sets all the characters of string1 to given


character 'B'.

strnset() strnset(string1, 'B', 5) Sets first 5 characters of string1 to given


character 'B'.

strrev() strrev(string1) It reverses the value of string1


1 // Example on Strings
2 #include<string.h>
3 int main()
4 {
5 char a[100],b[100]=“works”;
6
int n;
7
puts(“enter String”);
8
9 gets(a);
10 n=strlen(a);
11 printf(“ string length = %d”,n);
12 puts(strcat(a,b));
13 return 0;
14 }
15
16
17
18
19
20
21
OUTPUT:
Enter String
Logik
String length=5
Logikworks
Program 1
Write a C program to find whether the given string is palindrome or not

Sample input: Sample output:


Enter String palindrome
madam
1 int main() 23 return 0;
2 { 24 }
3 char s[100],s1[100];
4 int count=0,i,n;
5 printf(“enter String”);
6 scanf(“%s”,s);
7 n=strlen(s);
8 for(i=0;i<n;i++)
9 {
10 s1[i]=s[n-i-1];
11 }
12 for(i=0;i<n;i++)
13 {
14 if(s[i]==s1[i])
15 {
16 Count++;
17 }
18 }
19 if(count==n)
20 printf(“palindrome”);
21 else
22 printf(“Not a palindrome”);
Program 2
Write a C program to insert substring in main string at given postion.

Sample input: Sample output:


Enter first String Logikworks
Logworks
Enter second string
Ik
Enter the position where item to be insert
1 int main() 23 for(i=p;i<s;i++)
2 { 24 {
3 char a[10],b[10], c[10]; 25 x = c[i];
4 int p=0,r=0,i=0,t=0; 26 if(t<n)
5 int x,g,s,n,o; 27 {
6 puts("Enter First String:"); 28 a[i] = b[t];
7 gets(a); 30 t=t+1;
8 puts("Enter Second String:"); 31 }
9 gets(b); 32 a[o]=x;
10 printf("Enter the position where the 33 o=o+1;
11 item has to be inserted: "); 34 }
12 scanf("%d",&p); 35 printf("%s", a);
13 r = strlen(a); 36 return 0;
14 n = strlen(b); 37 }
15 i=0;
16 while(i <= r)
17 {
18 c[i]=a[i];
19 i++;
20 }
21 s = n+r;
22 o = p+n;

You might also like