There are 2 ways of using strings. The first is with a character array and the second is with a string pointer.
A character array is declared in the same way as a normal array.
char ca[10];
You must set the value of each individual element of the array to the character you want and you must make the last character a 0. Remember to use %s when printing the string.
char ca[10];
ca[0] = 'H';
ca[1] = 'e';
ca[2] = 'l';
ca[3] = 'l';
ca[4] = 'o';
ca[5] = 0;
printf("%s",ca);
String pointers are declared as a pointer to a char.
char *sp;
When you assign a value to the string pointer it will automatically put the 0 in for you unlike character arrays.
char *sp;
sp = "Hello";
printf("%s",sp);
You can read a string into only a character array using scanf and not a string pointer. If you want to read into a string pointer then you must make it point to a character array.
char ca[10],*sp;
scanf("%s",ca);
sp = ca; //no & in strings
scanf("%s",sp);
[All Possible Initializations:]
char name[20]="Hello World";
char name[20]={'h','e','l','l','o'};char name[20]={'h','e','l','l','o','\0'};
[NOT Possible :]
char name[20]; name="Hello York";
char name[20]; name="Hello";
[DRAWBACKS]
Reading Strings from the terminal:
The function scanf with %s format specification is needed to read the character string from the terminal.
Example:
char address[15];
scanf(“%s”,address);
Scanf statement has a draw back it just terminates the statement as soon as it finds a blank space, suppose if we type the string new york then only the string new will be read and since there is a blank space after word “new” it will terminate the string.
Note that we can use the scanf without the ampersand symbol before the variable name.
In many applications it is required to process text by reading an entire line of text from the terminal.
The function getchar can be used repeatedly to read a sequence of successive single characters and store it in the array.
We cannot manipulate strings since C does not provide any operators for string. For instance we cannot assign one string to another directly.
For example:
String=”xyz”;
String1=string2;
Are not valid. To copy the chars in one string to another string we may do so on a character tocharacter basis.
( use of gets() & puts() from stdio.h )
[STRING FUNCTIONS]
String handling functions
The strings.h header file has some useful functions for working with strings. Here are some of the functions you will use most often:
strcpy(destination,source)
You can't just use string1 = string2 in C. You have to use the strcpy function to copy one string to another. strcpy copies the source string to the destination string.
//s1 = "abc";
//s2 = "xyz";
strcpy(s1,s2); // s1 = "xyz"
strcpy(Name,”Robert”);
strcat(destination,source)Joins the destination and source strings and puts the joined string into the destination string.
//s1 = "abc";
//s2 = "xyz";
strcat(s1,s2); // s1 = "abcxyz"
strcmp(first,second)
Compares the first and second strings. If the first string is greater than the second one then a number higher than 0 is returned. If the first string is less than the second then a number lower than 0 is returned. If the strings are equal then 0 is returned.
//s1 = "abc";
//s2 = "abc";
i = strcmp(s1,s2); // i = 0
strcmpi() function
This function is same as strcmp() which compares 2 strings but not case sensitive.
Example
strcmpi(“THE”,”the”); will return 0.
strlen(string)
Returns the amount of characters in a string.
//s = "abcde";
i = strlen(s); // i = 5
strlwr () function:
This function converts all characters in a string from uppercase to lowercase.
syntax
strlwr(string);
For example:
//s = "ABC";
strlwr(s) //s = "abc";
strupr () function:
strupr(“exforsys”) will convert the string to EXFORSYS.
sprintf() function
Just came upon something new today. Just like how we use fprintf for files, similarly, for strings, we have sprintf() function.
sprintf( string_ptr, " Message %s ", String_variable_or_array);
EG:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
int main()
{
char a[10];
char b[10];
gets(a);
gets(b);
char c[20];
char *ptr;
ptr=c;
sprintf(ptr,"%s %s",a,b);
printf("\n HEllO %s \n",c);
}
An Alternative to using strcat() func
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
#include<string.h>
int main()
{
char a[20]; //a should be able to store concatenated value
char b[10];
gets(a);
gets(b);
char c[20];
// function concatenates string in a and b
// and saves it into variable a
strcat(a," ");
strcat(a,b);
printf("\n HEllO %s \n",a);
}
OUTPUT:
Neil
Mathew
HEllO Neil Mathew