String
String
Here. first ASCII of h, then ASCII of e, ASCII of l, ASCII of l. ASCII of o and last the \0
(NULL) character are stored. Hence, string with five characters needs six bytes of
storage. Once a string is stored, many operations can be performed on it. For
example.
• Printing a string
• Computing length of string
• Concatenation of two strings
• Comparing two strings
• Finding small string into large string
• Converting uppercase string to lowercase
• Reversing a string
String handling in C
This section discusses the declaration, initialization, reading and printing of the
strings in detail. The string is declared as follows.
char s[10);
where s is a string which can store maximum 9 characters as one byte is needed to
store the ‘/0' character. The string can also be initialized at the time of declaration
as follows.
char city[7] = "Rajkot";
The same can also be done as
char city[7] ( ‘R’ , ‘a’ , ‘j’ , ‘k’ , ‘o’ , ‘t’ , ‘\0' };
In above, the size is not compulsory and if not given, compiler automatically
computes and puts it.
Reading a string :
There are two functions to read a string : scanf() and gets(). The major difference
between them is that scanf() reads only upto first blank or tab while gets() reads
upto newline. Hence. scanf() is useful to read only words while gets() can be used
Prof. Maulik Kakadiya
Programming in C
to read strings with blanks and tabs also. The statement
scanf ("%s",name);
when user enters the following input from the keyboard
Maulik Kakadiya
assigns only first word i.e. "Maulik" to string variable name. The second word
"Kakadiya” remains into the buffer and subsequent scanf() may read it. The
statement
scanf("%s %s",fname,lname);
when user enters earlier input assigns "Maulik" to string fname and "Kakadiya" to
string lname. If total input "Maulik Kakadiya" is to be assigned .to a single string
name, the following gets() is used.
gets (name);
Printing a string
The printf() is used to print the string with %s. For example,
printf("%s",city);
prints the string stored in the string variable city.
• Example 1:
char str[10];
strncpy(str, "god", 3);
printf("%s", str); // Error: No null character to terminate string
• Example 2:
char str[10];
strcpy(str, "balagurusamy"); // Error: Overflow, array size
insufficient
1. Using puts()
#include <stdio.h>
int main() {
char str[] = "Hello, World!";
puts(str);
return 0;
}
2. Using printf()
#include <stdio.h>
int main() {
char str[] = "Hello, World!";
printf("Output using printf: %s\n", str);
return 0;
}
• 3. Using putchar()
o Prints a single character to the screen.
#include <stdio.h>
int main() {
char str[] = "Hello";
for(int i = 0; str[i] != '\0'; i++) {
putchar(str[i]);
}
putchar('\n');
Prof. Maulik Kakadiya
Programming in C
return 0;
}
4. Using fputs()
#include <stdio.h>
int main() {
char str[] = "Hello, World!";
fputs(str, stdout);
putchar('\n');
return 0;
}
Programs
1. Program to Compare Two Strings and Display Result
#include <stdio.h>
#include <string.h>
int main() {
char str1[50], str2[50];
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
if (strcmp(str1, str2) == 0)
printf("Strings are equal.\n");
else
printf("Strings are not equal.\n");
return 0;
}
Output: Enter first string: Hello
Enter second string: Hello
Strings are equal.
int main() {
char str[100];
printf("Enter a string: ");
gets(str);
printf("Length of string: %lu\n", strlen(str));
return 0;
}
Input and Output:
Prof. Maulik Kakadiya
Programming in C
Enter a string: “hello world”
Length of string: 11
Explanation:
The string "hello world" consists of 11 characters:
h, e, l, l, o, (space), w, o, r, l, d
The strlen() function returns the number of characters excluding the null character \0.
int main() {
char str1[50] = "Hello";
char str2[50] = " World";
char str3[50];
// strcat
strcat(str1, str2);
printf("Concatenated String: %s\n", str1);
// strcmp
if (strcmp(str1, str2) == 0)
printf("Strings are equal.\n");
else
printf("Strings are not equal.\n");
// strncpy
strncpy(str3, str1, 5);
str3[5] = '\0';
printf("Copied String using strncpy: %s\n", str3);
return 0;
}
Initial Values:
• str1 = "Hello"
• str2 = " World"
• str3 is an empty character array.
Final Output
Concatenated String: Hello World
Strings are not equal.
Copied String using strncpy: Hello
int main() {
char ch;
printf("Enter a string (Press Enter to stop): ");
while ((ch = getchar()) != '\n') {
putchar(ch);
}
return 0;
}
Input:
Hello World
Output:
Hello World
Explanation:
int main() {
char str[50];
printf("Enter your name: ");
gets(str);
The output of the given program will depend on the name entered by the user. The program
works as follows:
Example 1
Input:
Output:
Example 2
Input:
Explanation:
• Each character has a unique ASCII (American Standard Code for Information
Interchange) value.
• The program uses str[i] != '\0' to check for the null terminator and prints each
character along with its ASCII value using:
• ASCII values for uppercase letters (A-Z) range from 65 to 90 and lowercase letters
(a-z) range from 97 to 122.
int main() {
char str1[50], str2[50];
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
strcat(str1, str2);
printf("Joined String: %s\n", str1);
return 0;
}
Example 2
Input:
Enter first string: Good
Enter second string: Morning
Output:
Joined String: GoodMorning