Is there a way to replace all occurrences of a word in a string in C with another word. By word, I don't mean substring.
Here's what I want to achieve:
Input String:
OneOne One One OneOneOne One
Word to find:
One
Word to Replace it with:
Forty
Desired Output:
OneOne Forty Forty OneOneOne Forty
there are many example functions for replacing words in a string, i.e. What is the function to replace string in C?
you can use these functions to do what you want because the (white)spaces are also characters, see Removing Spaces from a String in C? and https://www.cs.tut.fi/~jkorpela/chars/spaces.html
so in the replace functions it is a difference if the replace string is
replace ='One' or replace = ' One '
if you use the second this should work
https://en.wikipedia.org/wiki/Whitespace_character
Unicode stored in C char
Related
consider a string="mat,cat,bat,dog,zebra,yumm"; how can i sort this string using the first letter of every word in the string. example- o/p of the above will be "bat,cat,dog,mat,yumm,zebra" ????
string []arrstr=str.split(",");
Arrays.sort(arrstr);
I am aware of INITCAP() and this similar question, but my question is different:
How can I uppercase the first letter only in a given string, and leave the rest of the string untouched?
INITCAP() can be made to uppercase the first letter only -- however, it will lowercase everything else, as mentioned in the documentation:
<delimiters> specified as an empty string (i.e. '') instructs INITCAP to ignore all delimiters, including whitespace characters, in the input expression (i.e. the input expression is treated as a single, continuous word). The resulting output is a string with the first character capitalized (if the first character is a letter) and all other letters in lowercase.
another option is
UPPER(LEFT(my_string,1))||SUBSTR(my_string,2)
and if we use the same whitespace pattern, would win the code golf by 1:
INSERT(my_string,1,1,UPPER(LEFT(my_string,1)))
Managed to do it using a few string manipulations:
SELECT INSERT(my_string, 1, 1, UPPER(LEFT(my_string, 1)));
UPPER(LEFT(my_string, 1)) will extract the first letter of the string, and uppercase it.
INSERT() will replace a portion of the string with something else -- in this case, the first letter will be replaced with its uppercase version.
I found this in some code and could not understand what it does:
scanf("%[^:]:%[^:]:%[^:\n]", a, b, c);
There was no mention of the [^:]: format string in the C documentation and I am quite confused.
The format string %[..] is for specifying the possible characters. For example %[A-DF] is for A, B, C, D, and F. And the ^ at the beginning is for any character excluding the characters specified. Hence, the first format string is for reading characters excluding colon. And the next is colon, and so on. You may test the scanf for the following input:
Adam:And:Apple
Important thing you should remember is, scanf() function accepts the arguments how it is specified.
For example:
scanf("%d %d",&a,&b);
assuming a and b are integer.
In the command line you have to input a and then followed by space and then b.because you have given a space in-between two "%d's". if you are giving n number of spaces are other character then you have to enter in-between those character before the value gets accepted.
Hence in your case:
you enter the string not containing ":" in it.
Consider this case, if you want to enter a string containing ":" in it.
For example:
"some:init" is the string then, in first %[^:] the string "some" is stored,then comes the ":" and then in second %[^:] it stores init. Then it wait for one more ":" and then the string not containing ":".
ultimately accepting the input in this format doesn't fetch any thing.
The only thing matters is how you print it on to the console.
I have a file, in which each line contains several words that are separated by variable amount of whitespace characters (spaces and tabs). For example:
do that param1 param2 param3
do this param1
(The number of words in a line is unknown in advance and is unbounded)
I'm looking for a way to parse such a line in plain C, so that I'll have a pointer to string containing the first word, a pointer to a string containing the second word, and a pointer to a string containing everything else (that is - all of the line, except the first two words). The idea is that the "rest of the line" string will be further parsed by a callback function, determined by the first two words).
Getting the first two words is easy enough (a simple sscanf), but I have no idea how to get the "rest of the line" pointer (As sscanf stops at whitespace, and I don't know the amount of whitespace before the first word, and between the first and the second word).
Any idea will be greatly appreciated.
You can use sscanf for the rest of the line as well. You just use a "scanset" conversion instead of a string conversion:
char word1[256], word2[256], remainder[1024];
sscanf(input_line, "%255s %255s %1023[^\n]", word1, word2, remainder);
writing another program, it reads a txt file, and stores all the letter characters and spaces (as \0) in a char array, and ignores everything else. this part works.
now what i need it to do is read a user inputted string, and search for that string in the array, then print the word every time it appears. im terrible at I/O in C, how do you read a string then find it in a char array?
#include <stdio.h>
...
char str [80];
printf ("Enter your word: ");
scanf ("%s",str);
char* pch=strstr(fileData,str);
while (pch!=NULL)
{
printf ("found at %d\n",pch-fileData+1);
pch=strstr(pch+1,str);
}
read in the user inputted string as a char array as well (cause strings are basically char* anyway in C)
use a string matching algorithm like Boyer-Moore or Knutt-Morris-Pratt (more popularly known as KMP) - google for it if you like for C implementations of these - cause they're neat, tried and tested ways of searching strings for substrings and pattern matches and all.
for each of these indexOf cases, print the position where the word is found maybe? or if you prefer, the number of occurrences.
Generally, the list of C string functions, found here, say, are of the format str* or strn*, depending on requirements.
One for-loop inside another for-loop (called nested loop). Go through all the letters in your array, and for each letter go through all the letters in your input string and find out if that part of the array matches with the input string. If it does, print it.