Showing posts with label C Strings. Show all posts
Showing posts with label C Strings. Show all posts

Thursday, July 5, 2018

C Strings

String in C language is an array of characters that is terminated by \0 (null character).

There are two ways to declare string in c language.

  1. By char array
  2. By string literal

Let's see the example of declaring string by char array in C language.





char ch[10]={'j', 'a', 'v', 'a''\0'};



As you know well, array index starts from 0, so it will be represented as in the figure given below.

---------------------------->>>>>>>>>>>>>>>>>

While declaring string, size is not mandatory. So you can write the above code as given below





char ch[]={'j', 'a', 'v', 'a','\0'};



You can also define string by string literal in C language. For example:





char ch[]="javatpoint";



In such case, '\0' will be appended at the end of string by the compiler.

Difference between char array and string literal

The only difference is that string literal cannot be changed whereas string declared by char array can be changed.

String Example in C

Let's see a simple example to declare and print string. The '%s' is used to print string in c language.





#include<stdio.h>
#include <string.h>
int main(){
char ch[11]={'j', 'a', 'v', 'a' , '\0'};
char ch2[11]="java";

printf("Char Array Value is: %s\n", ch);
printf("String Literal Value is: %s\n", ch2);
return 0;
}



Output:

Char Array Value is: java

String Literal Value is: java

Using pointers

String can also be initialized using pointers as





char *c = "abcd";



Reading Strings from user

You can use the scanf() function to read a string like any other data types.

However, the scanf() function only takes the first entered word. The function terminates when it encounters a white space (or just space).

Reading words from user





char c[20];
scanf("%s", c);



Using scanf() to read a string

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 ignores Ritchie because, scanf() function takes only a single string before the white space, i.e. Dennis.

Reading a line of text

An approach to reading a full line of text is to read and store each character one by one.

Using getchar() to read a line of text

1. C program to read line of text character by character.





#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;
}



In the program above, using the function getchar(), ch gets a single character from the user each time.

This process is repeated until the user enters return (enter key). Finally, the null character is inserted at the end to make it a string.

This process to take string is tedious.

Using standard library function to read a line of text

2. C program to read line of text using gets() and puts()

To make life easier, there are predefined functions gets() and puts in C language to read and display string respectively.





#include <stdio.h>
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 programs have the same output below:

Output

Enter name: Tom Hanks

Name: Tom Hanks

Passing Strings to Functions

Strings are just char arrays. So, they can be passed to a function in a similar manner as arrays.

Learn more about passing array to a function.





#include <stdio.h>
void displayString(char str[]);

int main()
{
char str[50];
printf("Enter string: ");
gets(str);
displayString(str); // Passing string c to function.
return 0;
}
void displayString(char str[]){
printf("String Output: ");
puts(str);
}



Here, string c is passed from main() function to user-defined function displayString(). In function declaration, str[] is the formal argument.

String handling functions

There are various string operations you can perform manually like: finding the length of a string, concatenating (joining) two strings etc.

But, for programmer's ease, many of these library functions are already defined under the header file <string.h>.




Instagram