Module 13 - part 1 - String Operations
Module 13 - part 1 - String Operations
operations
BITS Pilani Dr. Amitesh Singh Rajput
Pilani Campus
Department of Computer Science & Information Systems
Module Overview
• Strings
• Strings Operations
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Strings
• Strings in C are represented by arrays of characters
• End of the string is marked with a special character NULL.
• The corresponding escape sequence character is \0.
• C does not have string data type.
Declaration of strings:
char str[30];
char line[80];
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
String Initialization
char str[9] = “I like C”;
same as
char str[9]={‘I’,‘ ','l’,‘i’,‘k’,‘e’,‘ ’,‘C’,‘\0’};
char str1[]=“BITS”;
char str2[4]= “BITS”;
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Printing Strings
char text[] = “C Programming”;
printf("%s", text);
Output???
C Programming
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Reading a String (2-1)
Using scanf()
char text[30];
printf(“Enter a string: ”);
scanf(“%s”,text);
printf(“The string is : %s”, text);
Sample output:
Enter a string: hello
The string is: hello
-----------------------------------------
Enter a string: hello how are you
The string is: hello
Note: scanf() takes string without blank space
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Reading a String (2-2)
char text[30];
printf(“Enter a string: ”);
scanf(“%[a-z]s”, text);
printf(“The string is : %s”,text);
Sample output:
Enter a string: hello
The string is: hello
-----------------------------------------
Enter a string: hello123
The string is: hello
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Reading a String (2-3)
char text[80];
printf(“Enter a string: ”);
scanf(“%[^o]s”, text);
printf(“The string is : %s”, text);
Sample output:
Enter a string: Computer Programming
The string is: C
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Reading a String (2-4)
char text[180];
printf(“Enter a string: ”);
scanf(“%[^~]s”,text);
printf(“The string is : %s”,text);
Sample output:
Enter a string: hello~world
The string is: hello
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Input String using gets()
char str[200];
printf(“Enter a string: ”);
gets(str); Or fgets(str, 200, stdin);
printf(“The string is: %s”,str);
Output
Enter a string: C programming
The string is : C programming
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Character Manipulation in the
String – eliminate spaces
int main(){ Enter the text:
char s[80], ws[80]; Computer Programming
int i, j; The text without blank space is:
printf(“Enter the text:\n”); ComputerProgramming
gets(s); /* reading text from user */
Or fgets
for(i=0,j=0; s[i]!=‘\0‘; i++){
if(s[i]!=‘ ‘)
ws[j++] = s[i];
}
ws[j]=‘\0’;
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
String Manipulation functions
in string.h
strcpy(s1,s2) /* copies s2 into s1 */
strcat(s1,s2) /* concatenates s2 to s1 */
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Other functions in stdlib.h and
string.h
atof(): Converts an ASCII char s1[] = "+1776.23";
string to its floating-point double my_value = atof(s1);
equivalent (type double)
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Other functions in stdlib.h and
string.h
atoi(): Converts an ASCII char s2[] = "-23.5";
string to its integer equivalent int my_value = atoi(s2);
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Other functions in stdlib.h and
string.h
strncat(): Works like strcat, char s1[50] = "Hello, world!";
but concatenates only a char s2[] = "Bye now!";
strncat (s1, s2, 3);
specified number of
characters.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Other functions in string.h
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Other functions in string.h
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Other functions in string.h
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Implementation of strcat()
void main() /* s2 is concatenated after s1 */
{
char s1[100], s2[100];
int i = 0, j = 0;
printf("Enter first string\n");
scanf("%s",s1);
printf("Enter second string\n");
scanf("%s",s2);
while(s1[i++] != '\0');
i--;
while(s2[j] != '\0')
s1[i++] = s2[j++];
s1[i] = '\0';
printf("\n Final string is:%s",s1);
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Palindrome problem
void main(){
char str[80];
int left, right, i, len, flag = 1;
printf("Enter a string");
for(i = 0;(str[i] = getchar())!='\n'; i++);
len = i-1;
for(left = 0,right = len; left < right; ++left,--
right)
{ if(str[left]!= str[right])
{ flag = 0;
break;
}
}
if(flag)printf("\n String is palindrome");
else
printf("\n String is not a palindrome");
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Word counting Problem
void main(){
char text[40]; Isspace function is from library
int i = 0, count = 0; #include <ctype.h>
printf("Enter a string:");
gets(text); Or fgets
while(text[i] != '\0'){
while(isspace(text[i]))
i++; /* Repeat till first non blank character */
if(text[i] != '\0')
{
count++;
while(!isspace(text[i])&& text[i]!='\0')
i++; /* Repeat till first blank character */
}
}
printf("The number of words in the string is %d",count);
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Arrays of Strings
Declaration:
char name[5][30];
Five strings each contains maximum thirty characters.
Initialization:
char[5][10]={“One”,”Two”,”Three”,”Four”,”Five”};
char[5][]={“One”,”Two”,”Three”,”Four”,”Five”};
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Array of Strings
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Array of Strings using Pointers
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Exercise
int main () {
return 0;
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
String Arrays: Reading and
Displaying
void main(){
char name[5][30];
int i;
printf(“Enter five strings: \n”);
/* Reading strings */
for(i=0; i<5; i++)
scanf(“%s”, name[i]);
/* Printing strings */
for(i=0; i<5; i++)
printf(“\n%s”,name[i]);
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Array of Strings
Problem Statement: Write a C program that will read and store
the details of a list of students in the format
ID NAME MARKS
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Implementation (1-3)
#define N 5
#include<stdio.h>
#include<string.h>
int main()
{
char names[N][30], marks[N][10];
char id[N][12], temp[30];
int i, j;
/* Reading Student Details */
printf("Enter Student ID NAME and MARKS \n");
for(i = 0; i<N; i++)
scanf("%s %s %s", id[i], names[i], marks[i]);
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Implementation (2-3)
/* Alphabetical Ordering of Names */
for(i=1; i<=N-1; i++)
for(j=1; j<=N-i; j++)
if(strcmp(names[j-1], names[j])>0)
{ strcpy(temp, names[j-1]);
strcpy(names[j-1], names[j]);
strcpy(names[j], temp);
/* Swapping of marks */
strcpy(temp, marks[j-1]);
strcpy(marks[j-1], marks[j]);
strcpy(marks[j], temp);
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Implementation (3-3)
/* Swapping of ID’s */
strcpy(temp, id[j-1]);
strcpy(id[j-1], id[j]);
strcpy(id[j], temp);
}
printf("ALPHABETICAL LIST OF ID NAME & MARKS");
for(i=0; i<N; i++)
printf("%s\t%s\t %s\n",id[i],names[i],marks[i]);
return;
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Implementation for ID wise
Sorting
for(i=1; i<=N-1; i++){
for(j=1; j<=N-i; j++){
if(strcmp(id[j-1],id[j])>0){
strcpy(temp, id[j-1]);
strcpy(id[j-1], id[j]);
strcpy(id[j], temp);
/* Swaping of marks */
strcpy(temp, marks[j-1]);
strcpy(marks[j-1], marks[j]);
strcpy(marks[j], temp);
/* Swaping of names */
strcpy(temp, names[j-1]);
strcpy(names[j-1], names[j]);
strcpy(names[j], temp);
}
}
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Home Exercise 1
1) Write a C program to implement strcpy()
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus