Reading spaces on C - c

I'm trying to read a serie of characters like the following one in C:
&&&&&&&&\n& & & &\n& & &&&& &\n
(notice that in the beginning there are spaces too).
I'm using a cicle "while" with:
scanf("%c",&cvar);
because I'm "storing" the characters into an array of structs that i'm using.
However, the program keeps ignoring the first spaces, and only starts storing from the first '&' appearence.
Any tips? Thank you in advance and sorry for my poor english.
Here is part of the code:
while(scanf("%c",&lab[i].simb)!=EOF){
lab[i].x=x;
lab[i].y=y;
if(lab[i].simb=='\n'){
x=0;
y++;
} else x++;
i++;
}

Read this...
I'll suggest to use getline if you are using gcc, but if however you want to read each character use getc instead of scanf... and sorry for my poor English too...

Related

How can i read certain parts of of a text file and store them in character arrays to be compared?

here is the part of the program that i am struggling with, "You will need some kind of loop to read through the entries in a text file, for the 1st 4 fields in the text file, you will know that you are at the end of the field when you read a comma. For the last field in the text file, you will know you are at the end of the field when you read a newline.
I did the program using functions from string.h but was challenged do the same program without the use of string.h and i am stuck at the loop part of this program.
i know my loop is incorrect but i am having trouble figuring out the correct loop to use, any tips will be helpfully
do
{
ch = fgetc(fout);
if(ch == EOF)
{
break;
}
field[x]=ch;//loads all the characters into the array
printf("%c",field[x]);//printing
x++;
}while(1);//infinite loop
fclose(fout);
One way to solve this would be to make a 2dimensional array so a array of field and have an if in the loop that increments the variable that says which field array to use when there is a comma. You would end up with 5 char arrays that each have one field, and you'd have a \n in the last one Wich you then need to remove.
Hope this helps

Asking a user to input their name and outputting their initials in C

I just wanted to start this off by admitting I'm a complete beginner to coding, and that it's not something that comes intuitively to me.
What I'm trying to do is write a simple program that has the user input their full name, and outputs their initials. The logic I'm trying to follow is that since strings in C are just characters in an array, the characters that should be an initial will come after the '\0' value.
I'm not sure if my problem is a problem of logic or translating that logic into working syntax, so any help would be appreciated.
Here is the code in full:
# include <stdio.h>
#include <cs50.h>
#include <string.h>
int main (void)
{
printf ("Please insert your name. \n");
string name = get_string();
//printf ("Your name is %s\n", name);
int x = 0;
char c = name [x];
while (c != '\0')
{
x++;
}
printf ("%c/n", c);
}
I understand it's a complete mess, but again I'm trying to figure out if it's best to just quit, so any help would be appreciated.
Thanks in advance.
The logic I'm trying to follow is that since strings in C are just characters in an array, the characters that should be an initial will come after the '\0' value.
In C, \0 denotes the end of a string, so you definitely don't want to be looking for that value.
Let's think about the logic. Someone's initials are probably:
the first character in the string
the first character after a space
– i.e. "Albus Percival Wulfric Brian Dumbledore" -> "APWBD" –
so you'll want to loop over the string, looking for either:
a space, in which case you'll want to grab the next letter, or
the end of the string ('\0') in which case you'll want to stop.
Edge cases to watch out for:
what happens if the string is empty?
what happens if the string ends with a space? (this might not happen if you're guaranteed to get properly formatted input)
Please don't get discouraged – we were all beginners once. This kind of thinking isn't always straightforward, but the answer will come eventually. Good luck!

Starting a new line of text in C

I only have about 1 week of experience in coding, so I've got a lot to learn. I am currently trying to finish an assignment I have in my C class where I have to write a script that can output *s all around the text Welcome to C Programming.
I have managed to complete the first part of the assignment with the code I wrote below, but I can't seem to figure out how to get the *s above and below the text. There wasn't anything mentioned in my notes or lectures either so I'm pretty lost at the moment.
#include <stdio.h>
int main (void)
{
printf ("** Welcome to C Programming **");
return 0;
}
I work on a Mac if that helps. Thanks to anyone that can help me figure this out.
Edit: I added the new line tag and it's working now! Thank you everyone for your input. I am going to try practicing with all of the methods mentioned after I submit my assignment.
You can use the '\n' escape sequence to represent a newline (i.e. line-break) in your printf calls. Since your IDE/code editor most likely uses a monospaced font it should be pretty easy to align the * characters properly:
printf ("******************************\n");
printf ("** Welcome to C Programming **\n");
printf ("******************************\n");
Or, if you wanted to put the whole thing in a single printf call, you can use the \ character followed by a newline in a string literal to break the representation of the string in your editor over multiple lines:
printf (
"******************************\n \
** Welcome to C Programming **\n \
******************************\n"
);
Or even:
printf ("******************************\n"
"** Welcome to C Programming **\n"
"******************************\n");
There is a character for that. It's called "newline". You can't put it directly in the string because that would create a new line in the source code, but inside quotes in C you can produce it with \n.
Alternatively, instead of printf you could use puts, which prints a new line after the string. For this special case this may even be a better solution, since you are not using any of printf's features (the formatting).
As someone in the comments already mentioned - put the \n (new line character) in the text where you want a new line to occur. E.G.
printf("****\nWelcome to C Programming\n****");
****
Welcome to C Programming
****
#include <stdio.h>
int main (void)
{
printf ("******************************\n");
printf ("** Welcome to C Programming **\n");
printf ("******************************\n");
return 0;
}

How do I check if the line is over?

I ran into a problem today. I can't find a way to check if a line in a file is over and the words are read from the next one already. I read word by word from the file using fscanf, then process the word as I need to and print it out into another file but there is a problem.
for example my data file is:
Hello, how are you
doing?
and the result file shows:
Hello, how are you doing?
but i need the words to be in the same lines from which I took them. Please keep in mind that I need those words one by one, that is why I don't use getline()
here is my code of how I read words from the file:
while( fscanf(file, "%s", A) != EOF )
{
check(A, B, &a); // I edit the words and put them in B string
// which is printed to the write file
}
Thank you for any tips!
Read the line into a string with getline() or fgets(), then use sscanf to get the words out of this string.
You can use a simple logic instead, like matching strings like . or ? which generally ends lines.
You need to check for end of line by adding check.
As the end-of-line is represented by the newline character, which is '\n'. so in while loop instead of copying entire thing do it line by line with the help of check for '\n'

Why is putchar printing formatted output?

I've wanted to learn more about the C standard library, so I decided to implement printf using only the putchar function. I'd barely started when something odd happened. All I'd done was write a loop to print a verbatim copy of the format string, and then I realized that all the escape sequences, (\n, \t, etc.) had already been parsed and "properly" output.
Here's the minimal code:
int my_printf(const char* s){
size_t i;
char c;
for (i = 0; (c = *(s + i)) != '\0'; ++i){
putchar(c);
}
return 0;
}
int main(void){
my_printf("Here\t1\n0\n");
return 0;
}
I was expecting a literal Here\t1\n0\n to be output, but I instead got:
Here 1
0
Any idea why this happening? My first thought was that the compiler I'm using, (gcc), was trying to help by pre-analyzing the format string, but that seems odd, since it would cause a lot of problems, since it would break any char array. So, does anyone know why this is happening? And is this behavior defined in the standard? Thank you for any help!
Edit: As mafso stated in their answer, the replacements are done at compile time, and it is standard. Section 5.1.1.2.1.5 of the standard has the actual text.
The escape sequences are replaced at compile time, not at runtime by printf. "\n" is a string starting with a literal new-line character (it’s the only way to put a literal new-line character into a string).
The only part of the string printf interprets are conversion specification, which always start with a % sign, (and the 0-terminator, of course), every other character is printed literally. You don’t need to do anything further.

Resources