0% found this document useful (0 votes)
35 views

24) Explain About Strings With Program?: 1 G Viswanath, M.Tech, (PH.D), Associate Professor, Vemu Institute of Technology

Strings in C are arrays of characters terminated by a null character. Strings can be declared, initialized, read from the user, compared, concatenated, copied, and manipulated using standard string functions. Arrays in C, including strings, can be passed to functions by passing the array name, which passes the base address of the array to the function. Both single array elements and entire arrays can be passed to functions for processing.

Uploaded by

Music Tweets
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

24) Explain About Strings With Program?: 1 G Viswanath, M.Tech, (PH.D), Associate Professor, Vemu Institute of Technology

Strings in C are arrays of characters terminated by a null character. Strings can be declared, initialized, read from the user, compared, concatenated, copied, and manipulated using standard string functions. Arrays in C, including strings, can be passed to functions by passing the array name, which passes the base address of the array to the function. Both single array elements and entire arrays can be passed to functions for processing.

Uploaded by

Music Tweets
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

24) Explain about strings with program?

In C programming, array of characters is called a string. A string is terminated by a null


character /0.
Eg:
"c string tutorial"
Here, "c string tutorial" is a string. When, compiler encounters strings, it append a null
character /0 at the end of string.

Declaration of strings:-
Before we actually work with strings, we need to declare them first.
Strings are declared in a similar manner as arrays. Only difference is that, strings are of chartype.
char s[5];

Initialization of strings:-
In C, string can be initialized in a number of different ways.
For convenience and ease, both initialization and declaration are done in the same step.
char c[] = "abcd";
OR,
char c[50] = "abcd";
OR,
char c[] = {'a', 'b', 'c', 'd', '\0'};
OR,
char c[5] = {'a', 'b', 'c', 'd', '\0'};

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).
Eg:
char c[20];
scanf("%s", c);
//Write program to Using scanf() to read a string
#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
1
G VISWANATH, M.TECH, (Ph.D), ASSOCIATE PROFESSOR, VEMU INSTITUTE OF TECHNOLOGY
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.
//Write a program to compare the two strings
#include<stdio.h>
#include<conio.h>
void main()
{
char string1[20],string2[20];
int n;
clrscr();
printf("Enter string1,string2");
scanf("%s%s",string1,string2);
n=strcmp(string1,string2);
printf("Comparision of String=%d",n);
}
//Write a program to combined the two strings
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char string1[20],string2[20];
clrscr();
printf("Enter string1,string2");
scanf("%s%s",string1,string2);
strcat(string1,string2);
printf("Concatrate String=%s",string1);
}
//Write a program to copy the one string to another string
#include<stdio.h>
#include<conio.h>
void main()
{
char string1[20],string2[20];
clrscr();
printf("Enter string1");
scanf("%s",string1);
strcpy(string2,string1);
2
G VISWANATH, M.TECH, (Ph.D), ASSOCIATE PROFESSOR, VEMU INSTITUTE OF TECHNOLOGY
printf("String1=%s\tstring2=%s",string1,string2);
}
//Write a program find the length of given string
#include<stdio.h>
#include<conio.h>
void main()
{
char string[20];
int n;
clrscr();
printf("Enter string");
scanf("%s",string);
n=strlen(string);
printf("String Length=%d",n);
}
//Write a program to reverse the given string
#include<stdio.h>
#include<conio.h>
void main()
{
char string[20];
clrscr();
printf("Enter string");
scanf("%s",string);
strrev(string);
printf("String Reverse=%s",string);
}
//Find the given strings are equal or not
#include<stdio.h>
#include<conio.h>
void main()
{
char string1[20],string2[20];
int n;
clrscr();
printf("Enter string1,string2");
scanf("%s%s",string1,string2);
n=strcmp(string1,string2);
if(n==0)
printf("The strings are equal");
else
printf("The strings are not equal");
}
3
G VISWANATH, M.TECH, (Ph.D), ASSOCIATE PROFESSOR, VEMU INSTITUTE OF TECHNOLOGY
//Write a program to set the any symbol into given string
#include <stdio.h>
#include<conio.h>
#include <string.h>
int main(void)
{
char string[10] = "123456789";
char symbol = 'm';
clrscr();
printf("Before strset(): %s\n", string);
strset(string, symbol);
printf("After strset(): %s\n", string);
}
//Example of putc,getc
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[12],name[12];
clrscr();
puts("enter str,name");
gets(str); //reads only one variable
gets(name);
puts(str);
puts(name);
}
25) How to pass arrays to a function in C Programming?
In C programming, a single array element or an entire array can be passed to a function.
This can be done for both one-dimensional array or a multi-dimensional array.
Passing One-dimensional Array In Function
Single element of an array can be passed in similar manner as passing variable to a function.
// Write C program to pass a single element of an array to function
#include <stdio.h>
void display(int age)
{
printf("%d", age);
}
int main()
{
int ageArray[] = { 2, 3, 4 };
display(ageArray[2]); //Passing array element ageArray[2] only.
return 0;
4
G VISWANATH, M.TECH, (Ph.D), ASSOCIATE PROFESSOR, VEMU INSTITUTE OF TECHNOLOGY
}
Output
4
Passing an entire one-dimensional array to a function:-
While passing arrays as arguments to the function, only the name of the array is passed (,i.e,
starting address of memory area is passed as argument).
/*Write a C program to pass an array containing age of person to a function. This function should
find average age and display the average age in main function.*/
#include <stdio.h>
float average(float age[]);
int main()
{
float avg, age[] = { 23.4, 55, 22.6, 3, 40.5, 18 };
avg = average(age); /* Only name of array is passed as argument. */
printf("Average age=%.2f", avg);
return 0;
}
float average(float age[])
{
int i;
float avg, sum = 0.0;
for (i = 0; i < 6; ++i) {
sum += age[i];
}
avg = (sum / 6);
return avg;
}
Output
Average age=27.08

5
G VISWANATH, M.TECH, (Ph.D), ASSOCIATE PROFESSOR, VEMU INSTITUTE OF TECHNOLOGY

You might also like