Skip to main content

Posts

Showing posts with the label String in c language

C Program to Sort Elements in Lexicographical Order (Dictionary Order)

  Sort strings in the dictionary order # include <stdio.h> # include <string.h> int main () { char str[ 5 ][ 50 ], temp[ 50 ]; printf ( "Enter 5 words: " ); // Getting strings input for ( int i = 0 ; i < 5 ; ++i) { fgets(str[i], sizeof (str[i]), stdin ); } // storing strings in the lexicographical order for ( int i = 0 ; i < 5 ; ++i) { for ( int j = i + 1 ; j < 5 ; ++j) { // swapping strings if they are not in the lexicographical order if ( strcmp (str[i], str[j]) > 0 ) { strcpy (temp, str[i]); strcpy (str[i], str[j]); strcpy (str[j], temp); } } } printf ( "\nIn the lexicographical order: \n" ); for ( int i = 0 ; i < 5 ; ++i) { fputs (str[i], stdout ); } return 0 ; } Output Enter 5 words: R programming JavaScript Java C programming C++ programming In the lexicographical order: C programming C+...

C Program to Copy String Without Using strcpy()

  Copy String Without Using strcpy() # include <stdio.h> int main () { char s1[ 100 ], s2[ 100 ], i; printf ( "Enter string s1: " ); fgets(s1, sizeof (s1), stdin ); for (i = 0 ; s1[i] != '\0' ; ++i) { s2[i] = s1[i]; } s2[i] = '\0' ; printf ( "String s2: %s" , s2); return 0 ; } Output Enter string s1: Hey fellow programmer. String s2: Hey fellow programmer.

C Program to Concatenate Two Strings

  Concatenate Two Strings Without Using strcat() # include <stdio.h> int main () { char s1[ 100 ] = "programming " , s2[] = "is awesome" ; int length, j; // store length of s1 in the length variable length = 0 ; while (s1[length] != '\0' ) { ++length; } // concatenate s2 to s1 for (j = 0 ; s2[j] != '\0' ; ++j, ++length) { s1[length] = s2[j]; } // terminating the s1 string s1[length] = '\0' ; printf ( "After concatenation: " ); puts (s1); return 0 ; } Output After concatenation: programming is awesome

C Program to Remove all Characters in a String Except Alphabets

  Remove Characters in String Except Alphabets # include <stdio.h> int main () { char line[ 150 ]; printf ( "Enter a string: " ); fgets(line, sizeof (line), stdin ); // take input for ( int i = 0 , j; line[i] != '\0' ; ++i) { // enter the loop if the character is not an alphabet // and not the null character while (!(line[i] >= 'a' && line[i] <= 'z' ) && !(line[i] >= 'A' && line[i] <= 'Z' ) && !(line[i] == '\0' )) { for (j = i; line[j] != '\0' ; ++j) { // if jth element of line is not an alphabet, // assign the value of (j+1)th element to the jth element line[j] = line[j + 1 ]; } line[j] = '\0' ; } } printf ( "Output String: " ); puts (line); return 0 ; } Output Enter a string: p2'r-o@gram84iz./ Output String: progra...

C Program to Count the Number of Vowels, Consonants and so on

  Program to count vowels, consonants etc. # include <stdio.h> int main () { char line[ 150 ]; int vowels, consonant, digit, space; vowels = consonant = digit = space = 0 ; printf ( "Enter a line of string: " ); fgets(line, sizeof (line), stdin ); for ( int i = 0 ; line[i] != '\0' ; ++i) { if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' || line[i] == 'o' || line[i] == 'u' || line[i] == 'A' || line[i] == 'E' || line[i] == 'I' || line[i] == 'O' || line[i] == 'U' ) { ++vowels; } else if ((line[i] >= 'a' && line[i] <= 'z' ) || (line[i] >= 'A' && line[i] <= 'Z' )) { ++consonant; } else if (line[i] >= '0' && line[i] <= '9' ) { ++digit; } els...

C Program to Find the Frequency of Characters in a String

  Find the Frequency of a Character # include <stdio.h> int main () { char str[ 1000 ], ch; int count = 0 ; printf ( "Enter a string: " ); fgets(str, sizeof (str), stdin ); printf ( "Enter a character to find its frequency: " ); scanf ( "%c" , &ch); for ( int i = 0 ; str[i] != '\0' ; ++i) { if (ch == str[i]) ++count; } printf ( "Frequency of %c = %d" , ch, count); return 0 ; } Output Enter a string: This website is awesome. Enter a character to find its frequency: e Frequency of e = 4