1

Problem statement

Roy wanted to increase his typing speed for programming contests. So, his friend advised him to type the sentence "The quick brown fox jumps over the lazy dog" repeatedly, because it is a pangram. (Pangrams are sentences constructed by using every letter of the alphabet at least once.)

After typing the sentence several times, Roy became bored with it. So he started to look for other pangrams.

Given a sentence s, tell Roy if it is a pangram or not.

Input Format Input consists of a line containing s.

Constraints Length of s can be at most 103 (1≤|s|≤103) and it may contain spaces, lower case and upper case letters. Lower case and upper case instances of a letter are considered the same.

Output Format Output a line containing pangram if s is a pangram, otherwise output not pangram.

void panagram(char s[])
 {
int num1[26]={0};
int num2[26]={0};
int len=strlen(s);
int count=0,j,i;
for(i=0;i<len;i++)
{   
    if(s[i]>=97&&s[i]<=122)
    {
        num1[s[i]-97]++;
    }
    if(s[i]>=65&&s[i]<=90)
    {
        num2[s[i]-65]++;
    }


}
for(j=0;j<26;j++)
        {
            if(num1[j]>=1||num2[j]>=1)
            {   printf("%d\t\t%d\n",num1[j],num2[j]);
                count++;
            }
        }
printf("%d\t",count);
if(count>=26)
            printf("panagram");
            else
                printf("not panagram");
}
int main() {
    char s[1000];
     scanf("%s",s);
     panagram(s);
    return 0;
    }

The code works fine for strings without blank spaces like "Wepromptlyjudgedantiqueivorybucklesforthenextprize" but fails to work for strings with blank spaces - "We promptly judged antique ivory buckles for the next prize" Can anyone tell where I am going wrong? Am I taking input incorrectly?

Community
  • 1
  • 1
Ravi Kala
  • 33
  • 1
  • 6
  • possible duplicate of [reading a string with spaces with sscanf](http://stackoverflow.com/questions/2854488/reading-a-string-with-spaces-with-sscanf) – Mohit Jain Apr 29 '15 at 10:29

2 Answers2

3

As per the C11 standard,

7.21.6.2 The fscanf function

[...]

  1. The conversion specifiers and their meanings are:
    [...]
    s. Matches a sequence of non-white-space characters. 286
    [...]

You have

scanf("%s",s);

to take input. %s will stop scanning the input until it encounters a whitespace character. A space is a whitespace character as per the C11 standard:

6.4 Lexical elements

[...]

  1. [...]
    Preprocessing tokens can be separated by white space; this consists of comments (described later), or white-space characters (space, horizontal tab, new-line, vertical tab, and form-feed), or both
    [...]

So, when the %s sees the space after the first word, it stops scanning.


Fix the problem by using fgets as shown in @Gopi's answer or if you need to use scanf, use
scanf("%[^\n]",s);

The above scanf reads everything until it encounters a newline character. A better way would be to limit the amount of characters that scanf reads(a maximum of 999 in your case) in order to prevent buffer overflows and also, checking the return value of scanf to see if it was successful(The return value will be 1, in your case, if scanf was successful).

Note: The above fix, unlike fgets, will leave the newline character in the standard input stream(stdin).

Community
  • 1
  • 1
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
2

Instead of scanf() use

fgets(s,sizeof(s),stdin);

It is never a good idea to use scanf() while reading strings.So I suggest you to use fgets()

PS: fgets() comes with a newline character and suppress the newline character.

Gopi
  • 19,784
  • 4
  • 24
  • 36