Reading a two dimensional string using gets - c

int main(int argc, char const *argv[])
{
char str[100][100];
int n, i;
scanf("%d", &n);
for (i = 0; i < n; i++)
gets(str[i]);
for (i = 0; i < n; i++)
printf("%s\n",str[i]);
}
Why i'm unable to read the string properly?
raja#raja-Inspiron-N5110:~/myctry$ ./a.out
2
abc def
abc def

change
scanf("%d",&n);
to
scanf("%d\n",&n);
if you press enter to input your number. this form gets rid of the '\n' in str[1] when scanf().

You're using 1-based indexing. C uses 0-based indexing.
Change the for loop to for(i=0;i<n;i++)

Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.
MAN

There are several issues:
You need to start your for loop at index 0.
You should print your string out
int main(int argc, char const *argv[])
{
char str[100][100];
int n,i;
scanf("%d",&n);
for(i=0;i<n;i++) // start at i = 0
gets(str[i]); // store string to str[0]
printf("%s\n", str[0]); // print the results
}

Start loop from index 0 - you have started from 1
for (i = 0; i <= n; i++)
Moreover, you should print the string as well.

#include<string.h>
#include<stdio.h>
int main()
{
int n,i;
char str[100][100];
scanf("%d", &n);
for(i=0;i<n;i++)
{gets(str[i]);}
for(i=0;i<n;i++)
{printf("%s\n",str[i]); }
return 0;
}

As 7-isnotbad said,gets() is not safe,fgets() is a better choice.
Both gets() and fgets() read a line end by '\n' from buffer.
maybe you can try this code:
#include "stdio.h"
#define MAX_LINE 1024
#define MAX_ROW 100
int main()
{
char str[MAX_ROW][MAX_LINE];
int i,n;
printf("input the row of lines :\n");
scanf("%d\n",&n);
if(n < 1)
{printf("input error!\n");return -1;}
for(i = 0; i < n; ++i)
fgets(str[i],MAX_LINE - 1,stdin);
for(i = 0; i < n; ++i)
fputs(str[i],stdout);
return 0;
}

Related

How can I take input character in a character array using %c and produce the right output using %s?

This is my code.
#include<stdio.h>
#include<string.h>
int main()
{
int n, i;
char ch[100];
scanf("%d", &n);
for(i = 0; i < n; i++){
scanf(" %c", &ch[i]);
}
printf("%s\n", strupr(ch));
return 0;
}
At first, I want to take the size of the character array in n variable. After, i want to take n character's and assign the array. The output comes from this program is right but it also produce some garbage values.
For example:
5
s d g h f
Output: SDGHFC└U▄■`
How can i ignore the garbage values from my output?
Simply initialize your array ch[] to all zeros. I.E.
for (i = 0; i < 100; i += 1) { ch[i] = '\0'; }
Put this line just after the declaration of ch[].
As you are reading character the spaces you are providing in your input, will also be considered as characters, and strupr(c) will give some shaggy output, also you have to manually provided null character at the end of your character array. Below program might help you find your answer
#include<stdio.h>
#include<string.h>
int main()
{
int n, i;
scanf("%d", &n);
fflush(stdin);
char ch[100];
for(i = 0; i < n; i++){
char temp;
scanf("%c", &temp);
if(temp != '\n')
ch[i] = temp;
else
break;
}
ch[n] = '\0';
printf("%s\n", strupr(ch));
return 0;
}
Your Input should look like
5
sdghf
To give input with spaces. Program will look like.
#include<stdio.h>
#include<string.h>
int main()
{
int n, i;
scanf("%d", &n);
fflush(stdin);
char ch[100];
char temp;
i = 0;
while(scanf("%c", &temp)){
if(temp == ' ')
continue;
if(temp != '\n')
ch[i++] = temp;
else
break;
}
ch[i] = '\0';
printf("%s\n", strupr(ch));
return 0;
}
Now, you can give your character in any arrangement as you want.

Can't put Characters in an array of characters in C [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 4 years ago.
I'd like to create and manipulate an array of caracters. I don't want to use strings.
here is my code in C language :
int main(int argc, char *argv[]) {
char s[4];
int i;
for(i = 0; i < 4; i++){
printf("Character at %d : ",i);
scanf("%c",&s[i]);
printf("%c",s[i]);
}
return 0;
}
When I execute it, it seems that :
The compiler jumps from an element at i in the array to the element at i+2
Nothing is added in the array. the array stays empty
I'd like to understand what's wrong with the scanf("%c",&s[i]); that I think it is that instruction wich causes the problems in this code.
scanf() doesn't work as you expected. scanf() also considers the enter that you press as a character. If you are adamant about using scanf(), here are a couple of workaround for your current code.
Method 1
int main(int argc, char *argv[]) {
char s[4];
char enter;
int i;
for(i = 0; i < 4; i++) {
printf("Character at %d : ",i);
scanf("%c", &s[i]);
scanf("%c", &enter);
printf("%c \n", s[i]);
}
return 0;
}
Method 2
Use the same code, but enter all the four character at once.
int main(int argc, char *argv[]) {
char s[4];
int i;
for(i = 0; i < 4; i++) {
printf("Character at %d : ",i);
scanf("%c", &s[i]);
}
for(i = 0; i < 4; i++) {
printf("\n %c", s[i]);
}
return 0;
}
Output looks like:
Character at 0 : abcd
Character at 1 : Character at 2 : Character at 3 :
a
b
c
d
Method 3
As mentioned by Xing in comments, this looks better way to achieve it. But make sure you note the whitespace added before %c in scanf().
int main(int argc, char *argv[]) {
char s[4];
int i;
for(i = 0; i < 4; i++) {
printf("Character at %d : ",i);
scanf(" %c", &s[i]); // Note the whitespace before %c
printf("\n %c", s[i]);
}
return 0;
}
It doesn't work as you expected because scanf() takes only one character but it will only do that until you pressed enter. So the enter character is still in the buffer and to be read by the next iteration of scanf().
See How to clear input buffer in C? for suggestions on how to change the code.

Can I put a for loop inside scanf?

I want to have unknown amount of inputs in a single line. For example, user can input:
"ans: 1 2 3 4 5"
and scanf() will store these five numbers to an array. The problem is that the program don't know how many input will there be.
#include <stdio.h>
int main()
{
int i;
int input[4];
scanf("ans: " for(i = 0, i < 3,i++){scanf(" %d", &input[i]);};
return 0;
}
Sorry I'am totally new to coding, what will be the proper way to write this? Or is this impossible?
Thanks :)
Use fgets() and sscanf() with "%n"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char input[100];
int arr[10];
//fgets(input, sizeof input, stdin);
strcpy(input, "1 2 42 56 -3 0 2018\n"); // fgets
char *pi = input;
int tmp, pp, i = 0;
while (sscanf(pi, "%d%n", &tmp, &pp) == 1) {
if (i == 10) { fprintf(stderr, "array too small\n"); exit(EXIT_FAILURE); }
pi += pp;
arr[i++] = tmp;
}
printf("got this ==>");
for (int k = 0; k < i; k++) printf(" %d", arr[k]);
puts("");
}
You asked this question way round.
You can achieve what you expect by putting scanf inside of a loop.Even you can ask user to give how many inputs he want to enter.
#include <stdio.h>
int main()
{
int i;
int input[4];
printf("Enter the number of inputs you want to give : ");
scanf("%d", &n);
for(i = 0; i < n;i++)
{
printf("Enter the input number %d : ",i);
scanf("%d", &input[i]);
}
return 0;
}

Store & display user input in an array in C

I am trying to display and print a word from the user and store it into my array which is called
char word[20]
But I am having trouble. I know we use a "for loop" to scan it into the array but I keep going in circles and I believe the problem is with the i < 20.
I researched this and found that the answers to this are extremely experienced and I need a really basic way of doing it without the extra stuff. So all I want is to get word from the user, store it and print it onto the screen.
Can someone help without experienced code?
Code in C
char getWord(char word[]);
int main()
{
char word[20];
getWord(word);
return 0;
}
char getWord(char word[])
{
int i;
printf("Enter a word: ");
for (i = 0; i < 20; i++)
{
scanf(" %c", &word[i]);
}
return word;
}
All you want is
#include <stdio.h>
int main() {
char word[20];
scanf("%s", word); // Read and store
printf("%s\n", word); // Print
return 0;
}
You can use fgets and puts to read and write a string.
#include<stdio.h>
#define MAX 20
int main()
{
int ar[MAX], i, count;
fgets(ar, MAX, stdin); //it will accept whitespaces as well
puts(ar); //displaying entered string
return;
}
if you want to read via characters, ending character should be set to null character for it to be string.
char getWord(char word[]);
int main()
{
char word[20]
getWord(word);
printf("%s\n", word);
return 0;
}
char getWord(char word[])
{
int i;
char c;
printf("Enter a word: ");
for (i = 0; i < 19; i++)
{
scanf("%c", &c);
if ( c == '\n' )
break;
word[i]=c;
}
word[i]='\0';
return word;
}

How do I flush the i/o stream? I'm using scanf(), fflush(stdin) doesnt work

How can I flush the input buffer without putting a newline character in scanf()? Because my proffessor doesn't like it. I tried fflush(); but it didn't work.
#include <stdio.h>
#include <conio.h>
int CountUpper(char S[],int n)
{
int i,cntr = 0;
for(i = 0; i < n; i++)
if(S[i] >= 'A' && S[i] <= 'Z')
++cntr;
return cntr;
}
int main(void)
{
int n,i;
printf("Enter n: ");
scanf("%d",&n);
char array[n];
for(i = 0; i < n; i++)
{
scanf("%c",&array[i]);
//fflush(stdin);
}
printf("Number of uppercase characters in array: %d\n",CountUpper(array,n));
getch();
return 0;
}
fflush is defined only for output streams and fflush(stdin) invokes undefined behaviour.
You can look into this to discard inputs in buffer: what can I use to flush input?

Resources