Strings

What are String?

A string is a sequence of charachters that is treated as a single data item.

Examples:

                        "Hello World" 
"Enter the Number:"
"Sum = 12"
                    

Strings

Declaration

char string_name[size];



                        char city[10]; 
char message[15];
                    

String

Initialization

type array-name[size] = { list of values };


                        char city[7] = "Mumbai"; 
char city[7] = {'M','u','m','b','a','i','\0'};
                    

Note:

Size can be ommited. It will be computed from the number of elements in the initialization + 1 for NULL ($\0$) charachter.

                        char message[] = "Hello";                 // size is 6
char message[] = {'a','e','i','o','\0'};     // size is 5
                    
Null charachter $\0$ tells the compiler that the string has ended. The last element of the string should always be a NULL.

Note:

Partial Initialization

Compile time initialization can be partial. That is the number of initializers can be less than the declared size. In such cases the remaining elements are initialized with NULL ($ \0 $) in a string.

                        char message[5] = {'a','e' };           // 'a','e','\0','\0','\0'
char city[8] = "Delhi";     // 'D','e','l','h','i','\0','\0','\0'
                    

Reading Strings from Terminal

1. Using : $scanf()$

$ \%s $ and $ \%[ ] $ format specifiers is used to read data from terminal using scanf

                        char country[20];
scanf("%s",country);                            
scanf("%[^\n]",country);
                    
Note: $ \%s $ will read a word only. It stops reading when user enters space. In scanf when using charachter arrays as second parameter, & is not required.

Reading Strings from Terminal

2. Using : $gets()$
                        char country[20];
gets(country);
                    
gets can read sentences.

Which method is better?

$$ scanf(); $$

Or

$$ gets(); $$

Problem Statement #1

WAP to get a string from user and display it on screen.

Problem Statement #2

WAP to check if there is a vovel charachter in the entered string.

Problem Statement #3

Write a program in C to find the length of a string without using library function.

Problem Statement #4

Write a program in C to print individual characters of string in reverse order without using library function.

Home Work #1

Write a program in C to count the total number of words in a string.

Home Work #2

Write a program in C to copy one string to another stringwithout using library function.

Home Work #3

Write a program in C to remove special characters in String and keep only Alphabets and numbers.

Problem Statement #5

Write a program in C to check if the entered string is palindrome or not.

Displaying a String

                        printf("%s",a);
                    
                        puts(a);
                    

string.h Library

A set of utility functions for handling strings.

                        #include <string.h>
                    

Few of the functions in string.h

  • strlen()
  • strcpy()
  • strcat()
  • strcmp()
  • strlwr()
  • strupr()
  • strrev()

strlen(a)

The strlen(a) function computes the number of bytes in the string a, not including the terminating null byte.

                        #include <string.h>
char a[20] = "Mumbai";
printf("%d",strlen(a));     // 6
                        
                    

strcpy(b,a)

The strcpy() function copies string from a to string b by replacing the contents of b.

                        #include <string.h>
char a[20] = "Mumbai",b[20];
strcpy(b,a);
puts(b);
                        
                    

strcpy(destination,source);

strcat(a,b)

The strcat() function joins two string together. It joins the contents of string b to string a along with the null charachter.

                        #include <string.h>
char a[20] = "Hello ",b[20]="World";
strcat(a,b);
puts(a);
                        
                    

strcmp(a,b)

The strcat() function checks if both the strings are equal.

                        #include <string.h>
char a[20] = "Hello",b[20]="Hello";
if(strcmp(a,b)==0){
    printf("Same");
}else{
    printf("Different");
}
                        
                    

strlwr(a)

strlwr( ) function converts a given string into lowercase.

                        #include <string.h>
char a[20] = "HEllo";
strlwr(a);    // hello  
puts(a);
                        
                    

strupr(a)

strupr( ) function converts a given string into uppercase.

                        #include <string.h>
char a[20] = "HEllo";
strupr(a);    // HELLO  
puts(a);
                        
                    

strrev(a)

strrev( ) function reverses a given string in C language

                        #include <string.h>
char a[20] = "hello";
strrev(a);    // olleh  
puts(a);
                        
                    

stdlib.h Library

Few other important library function that may be helpful while programming

                        #include <stdlib.h>
                    

atoi(a)

This function returns the converted integral number as an int value. If no valid conversion could be performed, it returns zero.

                        #include <string.h>
int val;
char a[20] = "1900";
val = atoi(a);      
printf("%d",val);   // 1900
                        
                    

atof(a)

This function returns the converted integral number as an int value. If no valid conversion could be performed, it returns zero.

                        #include <string.h>
float val;
char a[20] = "19.3";
val = atof(a);      
printf("%f",val);   // 19.3
                        
                    

Problem Statement #6

Write a program in C to reverse a string.

Table of Strings

To create a list of strings. Multiple strings in one variable.

                        char names[5][20] = {
    "Amit",
    "Arun",
    "Junaid",
    "Shibu"                            
};     
printf("%s",names[0]);   // Amit
printf("%s",names[2]);   // Junaid