C is a procedural programming language. It was initially developed by Dennis Ritchie as a system programming language to write an operating system . The main features of the C language include low-level access to memory, a simple set of keywords, and a clean style, these features make C language suitable for system programmings like operating systems or compiler development. This article focuses on h ow to take a character, a string, and a sentence as input in C.
Reading a Character in C
Problem Statement#1: Write a C program to read a single character as input in C.
Syntax-
scanf("%c", &charVariable);
Approach-
Reading a Word in C
Problem Statement#2: Write a C program to read words as input from the user.
Syntax-
scanf("%s", stringvariable);
Approach-
Note:
An array name itself indicates its address. word == &word[0], these are both the same.It’s because the variable name word points to the first element of the array. So, there is no need to mention ampersand in scanf().
Reading a Sentence in C
Problem Statement#3: Write a C program to read sentences as input from the user.
Method 1-
Syntax-
scanf("%[^\n]s", sen)
scanf(“%[^\n]s”, sen) means to read a string including spaces until the next line is received or to read string until line break i.e. \n is encountered and store it on an array named “sen”.
It’ll stop reading after the first occurrence of that character f (specified in the scanset).
Method 2- Using fgets
Note- gets() never checks the maximum limit of input characters. Hence they may cause undefined behavior and probably lead to buffer overflow error which eventually causes the program to crash. Hence, it is advisable not to use the gets function to read strings. To overcome the above limitation, fgets can be used.
Syntax-
char *fgets(char *str, int size, FILE *stream)
Given two array of strings, arr1[] and arr2[], the task is to count the number of strings in arr2[] whose frequency of the smallest characters is less than the frequency of the smallest character for each string in arr1[]. Examples: Input: arr1[] = <"cbd">, arr2[] = Output: 1 Explanation: The frequency of the smallest characters in "cbd""cbd">
15 min read Find the word from a given sentence having given word as prefixGiven a string S representing a sentence and another string word, the task is to find the word from S which has the string word as a prefix. If no such word is present in the string, print -1. Examples: Input: S = "Welcome to Geeksforgeeks", word="Gee"Output: GeeksforgeeksExplanation:The word "Geeksforgeeks" in the sentence has the prefix "Gee". In
8 min read Print longest palindrome word in a sentenceGiven a string str, the task is to print longest palindrome word present in the string str.Examples: Input : Madam Arora teaches Malayalam Output: Malayalam Explanation: The string contains three palindrome words (i.e., Madam, Arora, Malayalam) but the length of Malayalam is greater than the other two.Input : Welcome to GeeksforGeeks Output : No Pa
14 min read Check if a word is present in a sentenceGiven a sentence as a string str and a word word, the task is to check if the word is present in str or not. A sentence is a string comprised of multiple words and each word is separated with spaces.Examples: Input: str = "Geeks for Geeks", word = "Geeks" Output: Word is present in the sentence Input: str = "Geeks for Geeks", word = "eeks" Output:
11 min read Find the word with most anagrams in a given sentenceGiven a string S in the form of a sentence, the task is to find the word from the text with the maximum number of its anagrams present in the given sentence. Example: Input: S = "please be silent and listen to what the professor says" Output: silent Explanation: Only the word "silent" has an anagram(listen) present in the sentence. Input: S = "cat
15 min read Remove last occurrence of a word from a given sentence stringGiven two strings S and W of sizes N and M respectively, the task is to remove the last occurrence of W from S. If there is no occurrence of W in S, print S as it is. Examples: Input: S = “This is GeeksForGeeks”, W="Geeks"Output: This is GeeksForExplanation:The last occurrence of “Geeks” in the string is substring over the range [16, 20]. Input: S=
11 min read Print each word in a sentence with their corresponding average of ASCII valuesGiven a sentence, the task is to find the average of ASCII values of each word in the sentence and print it with the word. Examples: Input: sentence = "Learning a string algorithm"Output:Learning - 102a - 97string - 110algorithm - 107 Approach: Take an empty string and start traversing the sentence letter by letter.Add a letter to the string and ad
6 min read Sums of ASCII values of each word in a sentenceWe are given a sentence of English language(can also contain digits), we need to compute and print the sum of ASCII values of characters of each word in that sentence. Examples: Input : GeeksforGeeks, a computer science portal for geeksOutput : Sentence representation as sum of ASCII each character in a word: 1361 97 879 730 658 327 527 Total sum -
7 min read Program for length of the longest word in a sentenceGiven a string, we have to find the longest word in the input string and then calculate the number of characters in this word. Examples: Input : A computer science portal for geeksOutput : Longest word's length = 8 Input : I am an intern at geeksforgeeksOutput : Longest word's length = 13 The idea is simple, we traverse the given string. If we find
7 min read Check input character is alphabet, digit or special characterAll characters whether alphabet, digit or special character have ASCII value. Input character from the user will determine if it's Alphabet, Number or Special character.ASCII value ranges- For capital alphabets 65 - 90For small alphabets 97 - 122For digits 48 - 57 Examples : Input : 8 Output : Digit Input : E Output : Alphabet C/C++ Code // CPP pro
3 min read Longest Common Prefix using Word by Word MatchingGiven a set of strings, find the longest common prefix. Examples: Input : <“geeksforgeeks”, “geeks”, “geek”, “geezer”>Output : "gee" Input : Output : "ap"Recommended PracticeLongest Common Prefix in an ArrayTry It! We start with an example. Suppose there are two strings- “geeksforgeeks” and “geeks”. What is the longest co“geeksforgeeks”,>
15 min read Word Ladder (Length of shortest chain to reach a target word)Given a dictionary, and two words 'start' and 'target' (both of same length). Find length of the smallest chain from 'start' to 'target' if it exists, such that adjacent words in the chain only differ by one character and each word in the chain is a valid word i.e., it exists in the dictionary. It may be assumed that the 'target' word exists in dic
15+ min read Javascript Program To Find Longest Common Prefix Using Word By Word MatchingGiven a set of strings, find the longest common prefix. Examples: Input : <“geeksforgeeks”, “geeks”, “geek”, “geezer”>Output : "gee"Input : Output : "ap"We start with an example. Suppose there are two strings- “geeksforgeeks” and “geeks”. What is the longest common prefix in both of them? It is “geeks”.Now let us introduce“geeksforgeeks”,>
3 min read Implementation of Tic-Tac-Toe for 2 person game (User vs. User)For 1-Person game (User vs. CPU), please refer Implementation of Tic-Tac-Toe game Rules of the Game The game is to be played between two people (in this program between HUMAN to HUMAN).First the game will take the names of the two players as input.One of the player chooses ‘O’ and the other ‘X’ to mark their respective cells.There would be a game t
9 min read Print the first and last character of each word in a StringGiven a string, the task is to print the first and last character of each word in a string.Examples: Input: Geeks for geeks Output: Gs fr gs Input: Computer applications Output: Cr as Approach Run a loop from the first letter to the last letter.Print the first and last letter of the string.If there is a space in the string then print the character
7 min read Remove the first and last character of each word in a stringGiven the string the task is to remove the first and last character of each word in a string.Examples: Input: Geeks for geeksOutput: eek o eek Input: Geeksforgeeks is bestOutput: eeksforgeek es Approach : Split the String based on the spaceRun a loop from the first letter to the last letter.Check if the character is the starting or end of the wordR
4 min read Input-output system calls in C | Create, Open, Close, Read, WriteSystem calls are the calls that a program makes to the system kernel to provide the services to which the program does not have direct access. For example, providing access to input and output devices such as monitors and keyboards. We can use various functions provided in the C Programming language for input/output system calls such as create, ope
10 min read How to read or input a string?In this article, we are going to learn how to print or output a string using different languages. Strings are considered a data type in general and are typically represented as arrays of bytes (or words) that store a sequence of characters. Strings are defined as an array of characters. Topics: How to Read or Input a String in C Scanf() How to Rea
3 min read Modify string by increasing each character by its distance from the end of the wordGiven a string S, the task is to modify the given string by replacing every character S[i] by a new character whose value is (S[i] + its position from the end of the word) Examples: Input: S = "acm fkz" Output: "cdm hlz" Explanation: There are 2 words in the given string <"acm", "fkz">For "acm": a becomes 'a' + 2 = 'c' c becomes 'c' + 1 = 'd' m be"acm",>
7 min readLast remaining character after repeated removal of the first character and flipping of characters of a Binary String
Given a Binary string str of length N, the task is to find the last character removed from the string by repeatedly removing the first character of the string and flipping all the characters of the string if the removed character is '0'. Examples: Input: str = "1001" Output: '0' Explanation: Removing str[0] from the string modifies str to "001". Re
5 min read Count substrings that starts with character X and ends with character YGiven a string str of n lowercase characters, the task is to count the number of substrings of str starting with character X and ending with character Y. Examples: Input: str = "abbcaceghcak" x = 'a', y = 'c' Output: 5 abbc, abbcac, ac, abbcaceghc, aceghc Input: str = "geeksforgeeks" x = 'g', y = 'e' Output: 6 Approach: Initialize two counters i.e.
6 min read Replace every character of a string by a different characterGiven a string str of lowercase alphabets only. The task is to replace every character by some new character. The new character belongs to [Tex]$a-z$ [/Tex](small case only) Replacement of str[i] is determined by: str[i] = Character obtained after traversing ascii(str[i]) no. of characters in 'a-z' repeatedly after str[i]. Examples: Input: str = "g
5 min read Shortest distance to every other character from given characterGiven a string S and a character X where [Tex]X\varepsilon S[i] [/Tex], for some [Tex]0\leq i \leq S.length()-1 [/Tex]. The task is to return an array of distances representing the shortest distance from the character X to every other character in the string. Examples: Input: S = "geeksforgeeks", X = 'e' Output: [1, 0, 0, 1, 2, 3, 3, 2, 1, 0, 0, 1,
15 min readCheck if frequency of character in one string is a factor or multiple of frequency of same character in other string
Given two strings, the task is to check whether the frequencies of a character(for each character) in one string are multiple or a factor in another string. If it is, then output "YES", otherwise output "NO". Examples: Input: s1 = "aabccd", s2 = "bbbaaaacc" Output: YES Frequency of 'a' in s1 and s2 are 2 and 4 respectively, and 2 is a factor of 4 F
6 min read Find the count of M character words which have at least one character repeatedGiven two integers N and M, the task is count the total words of M character length formed by the given N distinct characters such that the words have at least one character repeated more than once. Examples: Input: N = 3, M = 2 Output: 3 Suppose the characters are <'a', 'b', 'c'>All 2 length words that can be formed with these characters are "aa"'a',>
4 min readMap every character of one string to another such that all occurrences are mapped to the same character
Given two strings s1 and s2, the task is to check if characters of the first string can be mapped with the character of the second string such that if a character ch1 is mapped with some character ch2 then all the occurrences of ch1 will only be mapped with ch2 for both the strings. Examples: Input: s1 = "axx", s2 = "cbc" Output: Yes 'a' in s1 can
8 min read Count of substrings having the most frequent character in the string as first characterGiven a string S consisting of lowercase alphabets of size N, the task is to count all substrings which contain the most frequent character in the string as the first character. Note: If more than one character has a maximum frequency, consider the lexicographically smallest among them. Examples: Input: S = "abcab"Output: 7Explanation:There are two
7 min read Count substrings having frequency of a character exceeding that of another character in a stringGiven a string S of size N consisting of characters a, b, and c only, the task is to find the number of substrings of the given string S such that the frequency of character a is greater than the frequency of character c. Examples: Input: S = "abcc"Output: 2Explanation:Below are all the possible substrings of S(= "abcc") having the frequency of the
15 min read Kth character after replacing each character of String by its frequency exactly X timesGiven a string S consisting of N digits from [1, 9] and a positive integer K and X. Every day each character of the string is replaced by its frequency and value. The task is to find the Kth character of the string after X days. Examples: Input: S = "1214", K = 10, X = 3Output: 4Explanation:1st day = "12214444"2nd day = “1222214444444444444444”3rd
8 min read Repeat last occurrence of each alphanumeric character to their position in character family timesGiven a string str[] of size N, the task is to encode it in such a way that the last occurrence of each character occurs as long as its position in its family. As 'a' is the first character of its family (lower case alphabet), so it will remain 'a', but 'b' becomes 'bb', 'D' becomes 'DDDD' and so on. In case of numeric characters, the character occ