0% found this document useful (0 votes)
6 views4 pages

String in C

Uploaded by

sohamrana7777
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
6 views4 pages

String in C

Uploaded by

sohamrana7777
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 4

String in C

A string in C is simply an array of characters ended with null character (‘\


0’) This null character indicates the end of the string.
· Strings are always enclosed by double quotes. Whereas, character is
enclosed by single quotes in C.

EXAMPLE FOR C STRING:


· char string[10] = {‘h’, ’e’, ‘l’, ‘l’, ‘o’, ‘\0’};
(or)
· char string[10] = “Hello”;
(or)
· char string [] = “Hello”;

Difference between above declarations are, when we declare char as


“string[10]”, 10 bytes of memory space is allocated for holding the string value.
When we declare char as “string[]”, memory space will be allocated as per the
requirement during execution of the program.
If you do not palce null character at the end of string, the C compiler
automatically places the '\0' at the end of the string when it initializes the array.

#include <stdio.h>
int main () {
char string[10] = {'H', 'e', 'l', 'l', 'o', '\0'};
printf("Message is: %s\n", string);
return 0;
}
String Input and Output function:
Input function scanf() can be used with %s format specifier to read a string
input from the
terminal. But there is one problem with scanf() function, it terminates its input
on the first white space it encounters. Therefore if you try to read an input string
"Hello World"
using scanf() function, it will only read Hello and terminate after encountering
white spaces.
How to read a line of text?
You can use the gets() function to read a line of string. And, you can
use puts() to display the string.
Example : gets() and puts()
#include <stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
gets(name, sizeof(name), stdin); // read string
printf("Name: ");
puts(name); // display string
return 0;
}
Write a program to illustrate various string inbuilt functions (strcmp, strlen,
strcpy, strcat).
Program:
#include <stdio.h>
#include <string.h>
int main()
{
char name[10];
char name2[10] = "DEF";

strcpy(name, "ABC");
printf("\n Name is: %s",name);

strcat(name, name2);
printf( "\n strcat( name, name2): %s" , name );

int len;
len = strlen(name);
printf( "\n strlen(name) : %d",len);

if(strcmp(name,name2)==0)
printf(" \n Strings are equal");
else
printf(" \n Strings are not same");

return 0;
}

Write a program to count vowels in a string.


Program:

#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
unsigned short count = 0, vowels = 0;
char str[100], c;
printf("Enter a string in which you want to find number of vowels: ");
scanf("%[^\n]", str);
while(str[count] != '\0')
{
c = str[count];
if(c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I'
|| c == 'o' || c == 'O' || c == 'u' || c == 'U') {
vowels++;
printf("%c", c);
}
count++;
}
printf("\n");
printf("NUMBER OF VOWELS In Given string Are: %hu \n", vowels);
}
return 0;
}

Find Length of the String


#include <stdio.h>
int main() {
char str[100];
int length = 0;
// Get input from the user
printf("Enter a string: ");
fgets(str,sizeof(str),stdin);
// Loop through the string until we reach the null character '\0'
while (str[length] != '\0') {
length++;
}
// Display the length of the string
printf("Length of the string is: %d\n", length);
return 0;
}
Concatenate string
#include <stdio.h>
int main() {
char str1[100] = "Hello"; // First string with enough space for concatenation
char str2[100] = "World"; // Second string
int i = 0, j = 0;
// Move i to the end of the first string (i.e., at the null terminator '\0')
while (str1[i] != '\0') {
i++;
}
// Append characters from str2 to str1
while (str2[j] != '\0') {
str1[i] = str2[j]; // Copy character from str2 to str1
i++; // Move to the next position in str1
j++; // Move to the next character in str2
}
// Add the null terminator '\0' at the end of the concatenated string
str1[i] = '\0';
// Display the concatenated string
printf("Concatenated string: %s\n", str1);

return 0;
}
Compare two strings
#include <stdio.h>
int main() {
char str1[100], str2[100];
int i = 0, isEqual = 1; // isEqual is used to track if strings are equal
// Get two strings from the user
printf("Enter the first string: ");
fgets(str1,sizeof(str1),stdin);
printf("Enter the second string: ");
fgets(str2,sizeof(str2),stdin);
// Compare the strings character by character
while (str1[i] != '\0' || str2[i] != '\0') {
if (str1[i] != str2[i]) {
isEqual = 0; // If characters are different, mark as not equal
break;
}
i++;
}
// Check if strings are equal
if (isEqual == 1) {
printf("The strings are equal.\n");
} else {
printf("The strings are not equal.\n");
}
return 0;
}

You might also like