C, string input without spaces or tabs - c

I am trying to find the best way of getting an input of string without the spaces and tabs.
And from it to get dynamic number of the individual strings that the main one contian.
For example:
For the string str = " abc \t tt 6 \t 4 7"
(There can be a lot more spaces and tabs between the individual strings)
The out put will be str1 = "abc" str2 = "tt" str3 = "6" str4 = "4" str5 = "7"
I thought maybe for the dynamic creation of string to use malloc to creat an array of strings. But I could not make it work, and ignore the spaces and tabs (\t)

have a look to strtok() and strtok_r() functions from string.h
It allows you to split a string by specifying which characters are delimiters.

Related

Word Replace in C, not Substring

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

using strtok() in c with combined word

I'd like to know how to use strtok to find values, so is this possible to use strtok(mystring, "") or no?
I want split this : mystring --> %3456 I want split into 2 parts : "%" and "3456". Is this possible? how can I do that?
You cannot use strtok for this purpose: strtok will modify its first argument, overwriting the first separator with a '\0'.
Use strspn or strcscn() to scan for sequences of known characters, and copy the sequences into a separate buffer with memcpy.

Error in macro expansion

I have been trying to understand macro expansion and found out that the second printf gives out an error. I am expecting the second print statement to generate the same output as the first one. I know there are functions to do string concatenation. I am finding it difficult to understand why first print statement works and the second doesn't.
#define CAT(str1, str2) str1 str2
void main()
{
char *string_1 = "s1", *string_2 = "s2";
printf(CAT("s1", "s2"));
printf(CAT(string_1, string_2));
}
Concatenating string literals, like "s1" "s2", is part of the language specification. Just placing two variables next to each other, like string_1 string_2 is not part of the language.
If you want to concatenate two string variables, consider using strcat instead, but remember to allocate enough space for the destination string.
Try to do the preprocessing "by hand":
CAT is supposed to take 2 input variables, and print them one after the other, with a space between. So... if we preprocess your code, it becomes:
void main()
{
char *string_1 = "s1", *string_2 = "s2";
printf("s1" "s2");
printf(string_1 string_2);
}
While "s1" "s2" is automatically concatenated to "s1s2" by the compiler, string_1 string_2 is invalid syntax.

Writing printf(#"") with multiple line string

Is it possible to write something like this:
printf(#"
-
-
-
-
");
I can do it in C#, but can't in C. It gives me an error in CodeBlocks. Am I allowed to do such ?
Error message: error: stray '#' in program.
No. That syntax doesn't exist in C.
If you want a multiple-line string, write it as multiple double-quoted strings with no other tokens in between them. They will be combined.
printf(
"some string"
"more of the string"
"even more of the string"
);
(You will, of course, need to add a \n at the end of each line if that's what you want.)
No that's not a syntax that C understands, C doesn't have raw literals.
You can use \ as the last character to continue on the next line:
const char *str = "hello\n\
world";
Also, consecutive string literals will be concatenated. So you can do e.g.
const char *str = "Hello\n"
"world\n";
C#'s verbatim strings are not available in C. If you have some characters to escape, like " or \, escape them with '\', there is no there option in this language.
If you want to embed multiple lines in a string literal, you can either insert \n at the appropriate location in your string, or escape the return character as well:
printf("Here's\
a multiline\
string litteral");
Line continuation with \ at the end of the line.
printf("\
\
-\
-\
-\
-\
");
String literals in C may not contain newlines. You have two workarounds:
Use implicit string concatenation (done by the compiler).
printf("The quick brown"
" fox jumps over"
" the sleazy dog.");
Escape the newline by placing a backslash in front of it.
printf("The quick brown\
fox jumps over\
the sleazy dog.");
Personally, I prefer the first form since the second looks ugly (my opinion) and forces you to ruin your code indentation.
In either case, the string will simply not contain the newlines. So if you really meant for them to be there, you'll have to add them via \n.

Left-pad printf with spaces

How can I pad a string with spaces on the left when using printf?
For example, I want to print "Hello" with 40 spaces preceding it.
Also, the string I want to print consists of multiple lines. Do I need to print each line separately?
EDIT: Just to be clear, I want exactly 40 spaces printed before every line.
If you want the word "Hello" to print in a column that's 40 characters wide, with spaces padding the left, use the following.
char *ptr = "Hello";
printf("%40s\n", ptr);
That will give you 35 spaces, then the word "Hello". This is how you format stuff when you know how wide you want the column, but the data changes (well, it's one way you can do it).
If you know you want exactly 40 spaces then some text, save the 40 spaces in a constant and print them. If you need to print multiple lines, either use multiple printf statements like the one above, or do it in a loop, changing the value of ptr each time.
I use this function to indent my output (for example to print a tree structure). The indent is the number of spaces before the string.
void print_with_indent(int indent, char * string)
{
printf("%*s%s", indent, "", string);
}
int space = 40;
printf("%*s", space, "Hello");
This statement will reserve a row of 40 characters, print string at the end of the row (removing extra spaces such that the total row length is constant at 40).
Same can be used for characters and integers as follows:
printf("%*d", space, 10);
printf("%*c", space, 'x');
This method using a parameter to determine spaces is useful where a variable number of spaces is required. These statements will still work with integer literals as follows:
printf("%*d", 10, 10);
printf("%*c", 20, 'x');
printf("%*s", 30, "Hello");
Hope this helps someone like me in future.
If you want exactly 40 spaces before the string then you should just do:
printf(" %s\n", myStr );
If that is too dirty, you can do (but it will be slower than manually typing the 40 spaces):
printf("%40s%s", "", myStr );
If you want the string to be lined up at column 40 (that is, have up to 39 spaces proceeding it such that the right most character is in column 40) then do this:
printf("%40s", myStr);
You can also put "up to" 40 spaces AfTER the string by doing:
printf("%-40s", myStr);

Resources