String Programming


C Programming String

In C programming, array of character are called strings. A string is terminated by null character /0. For example:
"c string tutorial"
Here, "c string tutorial" is a string. When, compiler encounters strings, it appends null character at the end of string.
Memory diagram of strings in C programming

Declaration of strings

Strings are declared in C in similar manner as arrays. Only difference is that, strings are of char type.
char s[5];
Declaration of strings in C language
Strings can also be declared using pointer.
char *p

Initialization of strings

In C, string can be initialized in different number of ways.
char c[]="abcd";
     OR,
char c[5]="abcd";
     OR,
char c[]={'a','b','c','d','\0'};
     OR;
char c[5]={'a','b','c','d','\0'};
Initialization of strings in C programming
String can also be initialized using pointers
char *c="abcd";

Reading Strings from user.

Reading words from user.

char c[20];
scanf("%s",c);
String variable c can only take a word. It is beacause when white space is encountered, the scanf()function terminates.
Write a C program to illustrate how to read string from terminal.
#include <stdio.h>
int main(){
    char name[20];
    printf("Enter name: ");
    scanf("%s",name);
    printf("Your name is %s.",name);
    return 0;
}
Output
Enter name: Dennis Ritchie
Your name is Dennis.
Here, program will ignore Ritchie because, scanf() function takes only string before the white space.

Reading a line of text

C program to read line of text manually.
#include <stdio.h>
int main(){
    char name[30],ch;
    int i=0;
    printf("Enter name: ");
    while(ch!='\n')    // terminates if user hit enter
    {
        ch=getchar();
        name[i]=ch;
        i++;
    }
    name[i]='\0';       // inserting null character at end
    printf("Name: %s",name);
    return 0;
}
This process to take string is tedious. There are predefined functions gets() and puts in C language to read and display string respectively.
int main(){
    char name[30];
    printf("Enter name: ");
    gets(name);     //Function to read string from user.
    printf("Name: ");
    puts(name);    //Function to display string.
    return 0;
}
Both, the above program has same output below:
Output
Enter name: Tom Hanks
Name: Tom Hanks

Passing Strings to Functions

String can be passed to function in similar manner as arrays as, string is also an array. Learn more about passing array to a function.
#include <stdio.h>
void Display(char ch[]);
int main(){
    char c[50];
    printf("Enter string: ");
    gets(c);             
    Display(c);     // Passing string c to function.    
    return 0;
}
void Display(char ch[]){
    printf("String Output: ");
    puts(ch);
}
Here, string c is passed from main() function to user-defined function Display(). In function declaration, ch[] is the formal argument. 

String handling functions

You can perform different type of string operations manually like: finding length of string, concatenating(joining) two strings etc. But, for programmers ease, many library function are defined under header file <string.h> to handle these commonly used talk in C programming. You will learn more about string hadling function in next chapter.

Do While Example


#include <stdio.h>
int main () {
int i = 0;
int loop_count = 5;

printf("Case1:\n");
do {
printf("%d\n",i);
i++;
} while (i<loop_count);

printf("Case2:\n");
i=20;
do {
printf("%d\n",i);
i++;
} while (0);

printf("Case3:\n");
i=0;
do {
printf("%d\n",i);
} while (i++<5);

printf("Case4:\n");
i=3;
do {
printf("%d\n",i);
i++;
} while (i < 5 && i >=2);
return 0;
}
 
Case1:
 0
 1
 2
 3
 4
 Case2:
 20
 Case3:
 0
 1
 2
 3
 4
 5
 Case4:
 3
 4
# 

While loop example


#include <stdio.h>

int main () {

    int i = 0;
    int loop_count = 5;

    printf("Case1:\n");
    while (i<loop_count) {
        printf("%d\n",i);
        i++;
    }

    printf("Case2:\n");
    i=20;
    while (0) {
        printf("%d\n",i);
        i++;
    }

    printf("Case3:\n");
    i=0;
    while (i++<5) {
        printf("%d\n",i);
    }
    printf("Case4:\n");
    i=3;
    while (i < 5 && i >=2) {
        printf("%d\n",i);
        i++;
    }

    return 0;
}
 
 Case1:
0
1
2
3
4
Case2:
5.500000
4.500000
3.500000
2.500000
1.500000
0.500000
Case3:
2
3
4
Case4:
0
1
2
3
4
Case5:
Case6:
0
1
2
Case7:

For Loop example


#include <stdio.h>

int main () {

    int i = 0, k = 0;
    float j = 0;
    int loop_count = 5;

    printf("Case1:\n");
    for (i=0; i < loop_count; i++) {
        printf("%d\n",i);
    }

    printf("Case2:\n");
    for (j=5.5; j > 0; j--) {
        printf("%f\n",j);
    }

    printf("Case3:\n");
    for (i=2; (i < 5 && i >=2); i++) {
        printf("%d\n",i);
    }

    printf("Case4:\n");
    for (i=0; (i != 5); i++) {
        printf("%d\n",i);
    }

    printf("Case5:\n");
    /* Blank loop  */
    for (i=0; i < loop_count; i++) ;

    printf("Case6:\n");
    for (i=0, k=0; (i < 5 && k < 3); i++, k++) {
        printf("%d\n",i);
    }

    printf("Case7:\n");
    i=5;
    for (; 0; i++) {
        printf("%d\n",i);
    }

    return 0;
}
 Case1:
0
1
2
3
4
Case2:
5.500000
4.500000
3.500000
2.500000
1.500000
0.500000
Case3:
2
3
4
Case4:
0
1
2
3
4
Case5:
Case6:
0
1
2
Case7:
 
 

Conditional Operator

#include<stdio.h>
#include<conio.h>
main()
{
int a,b,x;
a=10;
b=15;
x=(a>b)? a:b ;
getch();
}
----------------------------------
if (a> b)
x=a;
else
x=b; 

Logical Operator


Write a C Program by using Logical AND and OR Operator

 

Logical Operators

Statement of C Program: This Program illustrates the logical AND and OR operations: 

#include<stdio.h>
#include<conio.h>
void main()
{
int a , b , c;
clrscr();
a = 4 , b = 3;

c = a && b;
b = a|| b || c;
a = a && b || c;
printf("%d" , a\n);
printf("%d" , b\n);
printf("%d" , c\n);
}

Output:
1
1
1


Example program for logical operators in C:

Output:

&& Operator : Both conditions are true
|| Operator : Only one condition is true
! Operator : Both conditions are true. But, status is inverted as false