Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

STRINGS IN C

1. C program to illustrate length of string

#include <stdio.h>
#include <string.h> output:
int main()
Geeks
{
char str[] = "Geeks"; Length of string str is 5
printf("%s\n", str);
int length = 0;
length = strlen(str);
printf("Length of string str is %d", length);
return 0;
}
2. // C program to read string from user
#include<stdio.h>
int main() Input
{ GeeksforGeeks
char str[50];
Output
scanf("%s",str);
printf("%s",str); GeeksforGeeks
return 0;
}
3. // C Program to take input string which is separated by whitespaces
#include <stdio.h>
int main()
{ Input
char str[20];
Geeks for Geeks
scanf("%s", str);
Output
printf("%s", str);
return 0; Geeks
}
We can use multiple methods to read a string separated by
spaces in C. The two of the common ones are:
 fgets() function to read a line of string
 gets() to read characters from the standard input (stdin) and
store them as a C string until a newline character or the
End-of-file (EOF) is reached.
We can also scanset characters inside the scanf() function
4. C program to illustrate fgets()
#include <stdio.h>
#define MAX 50
int main()
Input
{ Abc Def
char str[MAX];
fgets(str, MAX, stdin); Output
printf("String is: \n");
Abc Def
puts(str);
return 0;
}
5. C Program to take string separated by whitespace using scanset characters
#include <stdio.h>
int main()
Input
{
char str[20]; Geeks for Geeks
// using scanset in scanf
Output
scanf("%[^\n]s", str);
// printing read string Geeks for Geeks
printf("%s", str);
return 0;
}
Function Name Description
strlen(string_name) Returns the length of string name.
strcpy(s1, s2) Copies the contents of string s2 to string s1.
Compares the first string with the second
strcmp(str1, str2) string. If strings are the same it returns 0.
Concat s1 string with s2 string and the result
strcat(s1, s2) is stored in the first string.
strlwr() Converts string to lowercase.
strupr() Converts string to uppercase.
strstr(s1, s2) Find the first occurrence of s2 in s1.
6. C program to concatenate two strings using strcat function
#include <stdio.h>
#include <string.h>
int main() Output: Geeks for Geeks
{
char s[] = "Geeks";
char s1[] = "forGeeks";
strcat(s, s1);
printf("Final string is: %s ", s);
return 0;
}
7. C program to concatenating two strings
using function
int main()
#include <stdio.h> {
#include <string.h> char s[5000], s1[5000];
void concatenate_string(char* s, char* s1) printf("Enter the first string: ");
{
gets(s);
int i;
int j = strlen(s);
printf("Enter the second string: ");
for (i = 0; s1[i] != '\0'; i++) { gets(s1);
s[i + j] = s1[i]; concatenate_string(s, s1);
} printf("Concatenated String is: '%s'\n", s);
s[i + j] = '\0';
return 0;
return;
}
}
8. C Program to Demonstrate the use of strcmp() function
#include <stdio.h>
#include <string.h>
int main()
{
char* first_str = "Geeks";
char* second_str = "Geeks";
printf("First String: %s\n", first_str);
printf("Second String: %s\n", second_str);
printf("Return value of strcmp(): %d",strcmp(first_str, second_str));
return 0;
}
9. C program to reverse a string
#include <stdio.h>
#include <string.h>
int main()
{
char str[40];
printf (" \n Enter a string to be reversed: ");
scanf ("%s", str);
printf (" \n After the reverse of a string: %s ", strrev(str));
return 0;
}
10. C implementation to check if a given string is palindrome or not
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = { "abbba" };
int l = 0;
int h = strlen(str) - 1;
while (h > l) {
if (str[l++] != str[h--]) {
printf("%s is not a palindrome\n", str);
return 0;
}
}
printf("%s is a palindrome\n", str);
return 0;
11. C implementation to check if a given string is palindrome or not
#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100];
printf("Enter a string to check if it's a palindrome\n");
gets(a);
strcpy(b, a); // Copying input string
strrev(b); // Reversing the string
if (strcmp(a, b) == 0) // Comparing input string with the reverse string
printf("The string is a palindrome.\n");
else
printf("The string isn't a palindrome.\n");
return 0; }

You might also like