Print string
#include < stdio.h >
int main()
{
char array[100];
printf("Enter a string\n");
scanf("%s", array);
printf("You entered the string %s\n",array);
return 0;
}
String length
#include < stdio.h >
#include < string.h >
int main()
{
char a[100];
int length;
printf("Enter a string to calculate it's length\n");
gets(a);
length = strlen(a);
printf("Length of entered string is = %d\n",length);
return 0;
}
Compare strings
#include < stdio.h >
#include < string.h >
int main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
if( strcmp(a,b) == 0 )
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
return 0;
}
Concatenate strings
#include < stdio.h >
#include < string.h >
int main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
strcat(a,b);
printf("String obtained on concatenation is %s\n",a);
return 0;
}
Reverse string
#include < stdio.h >
#include < string.h >
int main()
{
char arr[100];
printf("Enter a string to reverse\n");
gets(arr);
strrev(arr);
printf("Reverse of entered string is \n%s\n",arr);
return 0;
}
Find palindrome
#include < stdio.h >
#include < string.h >
int main()
{
char a[100], b[100];
printf("Enter the string to check if it is a palindrome\n");
gets(a);
strcpy(b,a);
strrev(b);
if( strcmp(a,b) == 0 )
printf("Entered string is a palindrome.\n");
else
printf("Entered string is not a palindrome.\n");
return 0;
}
Change case
Lower Case
#include < stdio.h >
#include < string.h >
int main()
{
char string[] = "Strlwr in C";
printf("%s\n",strlwr(string));
return 0;
}
Upper Case
#include < stdio.h >
#include < string.h >
int main()
{
char string[] = "strupr in c";
printf("%s\n",strupr(string));
return 0;
}
Delete vowels
#include < stdio.h >
#include < string.h >
int check_vowel(char);
int main()
{
char s[100], t[100];
int i, j = 0;
printf("Enter a string to delete vowels\n");
gets(s);
for(i = 0; s[i] != '\0'; i++) {
if(check_vowel(s[i]) == 0) {
/* not a vowel */
t[j] = s[i];
j++;
}
}
t[j] = '\0';
strcpy(s, t);
printf("String after deleting vowels: %s\n", s);
return 0;
}
int check_vowel(char c)
{
switch(c) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
return 1;
default:
return 0;
}
}
C substring
#include < stdio.h >
#include < malloc.h >
char* substring(char*, int, int);
int main()
{
char string[100], *pointer;
int position, length;
printf("Enter a string\n");
gets(string);
printf("Enter the position and length of substring\n");
scanf("%d%d",&position, &length);
pointer = substring( string, position, length);
printf("Required substring is \"%s\"\n", pointer);
free(pointer);
return 0;
}
char *substring(char *string, int position, int length)
{
char *pointer;
int c;
pointer = malloc(length+1);
if (pointer == NULL)
{
printf("Unable to allocate memory.\n");
exit(EXIT_FAILURE);
}
for (c = 0 ; c < position -1 ; c++)
string++;
for (c = 0 ; c < length ; c++)
{
*(pointer+c) = *string;
string++;
}
*(pointer+c) = '\0';
return pointer;
}
Sort a string
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
void sort_string(char*);
int main()
{
char string[100];
printf("Enter some text\n");
gets(string);
sort_string(string);
printf("%s\n", string);
return 0;
}
void sort_string(char *s)
{
int c, d = 0, length;
char *pointer, *result, ch;
length = strlen(s);
result = (char*)malloc(length+1);
pointer = s;
for ( ch = 'a' ; ch < = 'z' ; ch++ )
{
for ( c = 0 ; c < length ; c++ )
{
if ( *pointer == ch )
{
*(result+d) = *pointer;
d++;
}
pointer++;
}
pointer = s;
}
*(result+d) = '\0';
strcpy(s, result);
free(result);
}
These are the Concept of C programming codes... We'll be glad if you share your valuable information with us regarding this topic, then it will be a golden oppertunity for all of us to improve ourselves and know more. Comment below!!
Comments
Post a Comment