-2

I want the string to be printed till character ('e') comes.
Code which I tried:-

#include <stdio.h>
    int main() {
    int a,i,x;
    char b[10];
    char ch;
    //enter input string
    for(i=0;i<10;i++)
      scanf("%c",&b[i]);

    for(i=0;i<10;i++)
       if(b[i]!='e')
           printf("%c",b[i]); 

    return 0;
    }

Input:abcdefghij
Actual output:abcdfghij
Desired output:abcd
Question : Where am I wrong ? Will putting a break inside if block work here?

chunky
  • 75
  • 1
  • 2
  • 11
  • 1
    To me your input and output are exactly the same, so I'm not sure why you need a program at all. And the "limit" thing is a bit unclear. – sidyll Jan 06 '17 at 20:01
  • 3
    To start with, `for(i=0;i<=100;i++)` will not only exceed the array bounds, but leave no room to put mandatory string terminator `'\0'` after the characters that is expected of what you call a "string". – Weather Vane Jan 06 '17 at 20:01
  • 2
    Why the loops? Call scanf with %s. Not at all clear what you are trying to accomplish. – OldProgrammer Jan 06 '17 at 20:03
  • 2
    `scanf(" %s", b)` would scan elements till newline character is entered and `printf("%s", b)` would print the string. – Cherubim Jan 06 '17 at 20:04
  • @sidyll, This code part of my another code, in this I wants to print string till only I enter.For ex. input- qwert, then output should be-qwert, means I will have to store value 4(beacuse here length is 4) in some var. in first for loop and then I will run second for loop 4 time, so my question is how to store that 4 in first loop? – chunky Jan 06 '17 at 20:09
  • 3
    @Cherubim `scanf` with %s is unsafe; you might end up overflowing the stack (in this example). You can limit the number of read characters like this: `scanf ("%99s", b);`, but it's best to avoid it. – Torkel Bjørnson-Langen Jan 06 '17 at 20:14

5 Answers5

3

This is much cleaner if you want to use scanf.

#include <stdio.h>

int main()
{
   char b[101];

   scanf("%100s", b);

   printf("%s\n", b);

   return(0);
}

Or even better.

#include <stdio.h>

#define MAX_LENGTH 100

int main()
{
   char b[MAX_LENGTH+1]; // add 1 for the terminating zero

   scanf("%100s", b);

   printf("%s\n", b);

   return(0);
}

This one uses fgets to read the entire line.

#include <stdio.h>

#define MAX_LENGTH 100

int main()
{
   char b[MAX_LENGTH];

   fgets(b, MAX_LENGTH, stdin);

   printf("%s", b);

   return(0);
}
Mike Collins
  • 402
  • 3
  • 13
  • Where is space for the terminator? – OldProgrammer Jan 06 '17 at 20:44
  • 2
    Is is unfortunate that you cannot use `MAX_LENGTH` in `scanf`. (If you used `fgets` then you could pass `sizeof b`.) But [this answer](http://stackoverflow.com/a/3313798/4142924) shows how to limit the input length of `scanf` programmatically. – Weather Vane Jan 06 '17 at 21:17
  • 1
    With OP's code, or nearly so, user could enter "Mike Collins" and print out "MikeCollins". With this code, all it prints is "Mike\n". – chux - Reinstate Monica Jan 06 '17 at 21:24
  • @WeatherVane Of use [Stringification of a macro value](http://stackoverflow.com/questions/2653214/stringification-of-a-macro-value) - at compile time. – chux - Reinstate Monica Jan 06 '17 at 21:25
  • @WeatherVane To use `MAX_LENGTH` in `scanf()`, stringification example: `#define STRINGIFY_HELPER(a) #a` `#define STRINGIFY(a) STRINGIFY_HELPER(a)` `scanf("%" STRINGIFY(MAX_LENGTH) "s", b);` – chux - Reinstate Monica Jan 06 '17 at 21:46
  • A better approach might be to use getchar or getc inside the loop if you don't want to stop on whitespace. Using scanf seems to be overkill if we're only reading one character at a time. Even better, just use fgets() to read the entire line and then parse as necessary. – Mike Collins Jan 06 '17 at 21:53
  • @Mike Collins, If I want stop printing after a particular character then what check should I place? – chunky Oct 07 '17 at 06:06
  • You would need to check each line after is is read in. The disadvantage is that it would read the entire line before checking. However, it MIGHT still be faster than reading one character at a time.In any case I don't think the performance would be significant. – Mike Collins Feb 17 '18 at 18:07
2

How to print a string till limit?

What code should do is use fgets().

Avoid using scanf(). Is is too easy to use wrong.

#include <stdio.h>
#include <string.h>

int main() {
  char b[100];
  if (fgets(b, sizeof b, stdin)) {

    // If code needs to lop off the potential \n at the end
    b[strcspn(b, "\n")] = '\0';

    printf("%s\n", b);
  }
  return 0;
}

Advanced issues include how to handle excessively long input lines and error handling - not shown here.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

There are several mistakes!

  1. If you are initializing your loops from 0 then you need to set the condition till i<100.

  2. Change your format specifiers to %s.

  3. Change your IF statement to if(b[i]!='\0').

Dreamer
  • 112
  • 1
  • 14
1

Here is what you need to do

#include <stdio.h>

int main() 
{

    int a,i,x;
    char b[10];
    char ch; 

    //enter input string
    for(i=0;i<10;i++)
    {
        scanf("%c",&b[i]);
    }

    for(i=0;i<10;i++)
    {
        if(b[i]=='e')
        {
            break;
        }
    }
    return 0;
}

re

0
#include <stdio.h>

int main()
{

    int i;
    char b[10];

    for(i=0;i<10;i++)
    {
        scanf("%c",&b[i]);
    }

    for(i=0;i<10;i++)
    {
        if(b[i]=='e')
        {
            break;
        }
        printf("%c",b[i]);

    }
    return 0;
}
Yunus Temurlenk
  • 4,085
  • 4
  • 18
  • 39