Ticker

6/recent/ticker-posts

Strings

 

Chapter 9: Strings



 




Strings in C are sequences of characters terminated
by a null character ('\0'). They are used to represent textual data. In this
chapter, we will explore how to declare, initialize, and perform operations on
strings:



 



9.1
String Declaration and Initialization:



To declare a string in C, you can use an array of
characters. Strings can be initialized at the time of declaration or assigned
later.



 





char
string_name[size];





 



Example:





#include
<stdio.h>



int
main() {



    char greeting[10] = "Hello";



    char name[] = "John";



 



    printf("%s, %s!\n", greeting,
name);



 



    return 0;



}





 



The program declares and initializes two strings:
`greeting` and `name`. It then prints a formatted string using `printf()`.
Compile and run the program to see the output.



 



9.2
String Input:



You can use the `scanf()` function to read strings
from the user. However, you need to be cautious about the size of the input to
avoid buffer overflow.



 



Example:





#include
<stdio.h>



int
main() {



    char name[20];



 



    printf("Enter your name: ");



    scanf("%s", name);



 



    printf("Hello, %s!\n", name);



 



    return 0;



}





 



 



The program prompts the user to enter their name
using `printf()`. It reads the input using `scanf()` and stores it in the
`name` array. It then prints a greeting message using `printf()`. Compile and
run the program to test it.



 



9.3
String Functions:



C provides several built-in functions to perform
operations on strings. Here are a few commonly used string functions:



 



·        
`strlen()`: Returns the length of a
string.



·        
`strcpy()`: Copies one string to
another.



·        
`strcat()`: Concatenates two strings.



·        
`strcmp()`: Compares two strings.



 



Example:





#include
<stdio.h>



#include
<string.h>



int
main() {



    char str1[20] = "Hello";



    char str2[20] = "World";



 



    printf("Length of str1: %d\n",
strlen(str1));



    strcpy(str1, str2);



    printf("Concatenated strings:
%s\n", strcat(str1, str2));



    printf("Comparison result: %d\n",
strcmp(str1, str2));



 



    return 0;



}





 



 



The program demonstrates the usage of `strlen()`,
`strcpy()`, `strcat()`, and `strcmp()`. Compile and run the program to see the
output.



 



9.4
Working with Individual Characters:



You can access individual characters in a string
using the array notation and perform operations on them.



Example:





#include
<stdio.h>



int
main() {



    char str[] = "Hello";



 



    printf("First character: %c\n",
str[0]);



    str[0] = 'J';



    printf("Modified string: %s\n",
str);



 



    return 0;



}





 



 



The program accesses and prints the first character
of the `str` string. It then modifies the first character and prints the
modified string. Compile and run the program to see the output.



 



9.5
Input with Spaces:



By default, `scanf()` considers space as a
delimiter, so it won't read strings with spaces correctly. To read strings with
spaces, you can use `fgets()`.



 



Example:





#include
<stdio.h>



int
main() {



    char name[50];



 



    printf("Enter your name: ");



    fgets(name, sizeof(name), stdin);



 



    printf("Hello, %s!\n", name);



 



    return



 



 0;



}





 



 



The program uses `fgets()` to read the name input,
which can include spaces. It then prints a greeting message using `printf()`.
Compile and run the program to test it.







 

Post a Comment

0 Comments