0

I was trying to remove spaces of a string after scanning it. The program has compiled perfectly but it is not showing any output and the output screen just keeps getting shut down after scanning the string. Is there a logical error or is there some problem with my compiler(I am using a devc++ compiler btw). Any kind of help would be appreciated

int main()
{
    char str1[100];
    scanf("%s",&str1);
    int len = strlen(str1);
    int m;
    for (m=0;m<=len;){
        if (&str1[m]==" "){
            m++;
        }
        else {
            printf("%c",&str1[m]);
        }
        m++;
    }
    return 0;
}

Edit : sorry for the error of m=1, I was just checking in my compiler whether that works or not and just happened to paste that code

5 Answers5

2

Your code contains a lot of issues, and the behaviour you describe is very likely not because of a bug in the compiler :-)

Some of the issues:

Use scanf("%s",str1) instead of scanf("%s",&str1). Since str1 is defined as a character array, it automatically decays to a pointer to char as required.

Note that scanf("%s",str1) will never read in any white space because "%s" is defined as skipping leading white spaces and stops reading in when detecting the first white space.

In for (m=1;m<=len;) together with str1[m], note that an array in C uses zero-based indizes, i.e. str1[0]..str1[len-1], such that m <= len exceeds array bounds. Use for (m=0;m<len;) instead.

Expression &str1[m]==" " is correct from a type perspective, but semantically a nonsense. You are comparing the memory address of the mth character with the memory address of a string literal " ". Use str1[m]==' ' instead and note the single quotes denoting a character value rather than double quotes denoting a string literal.

Statement printf("%c",&str1[m]) passes the memory address of a character to printf rather than the expected character value itself. Use printf("%c",str1[m]) instead.

Hope I found everything. Correct these things, turn on compiler warnings, and try to get ahead. In case you face further troubles, don't hesitate to ask again.

Hope it helps a bit and good luck in experiencing C language :-)

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
2

There are many issues:

  1. You cannot read a string with spaces using scanf("%s") , use fgets instead (see comments).
  2. scanf("%s", &str1) is wrong anyway, it should be scanf("%s", str1);, str1 being already the address of of the string
  3. for (m = 0; m <= len;) is wrong, it should be for (m = 0; m < len;), because otherwise the last character you will check is the NUL string terminator.
  4. if (&str1[m]==" ") is wrong, you should write if (str1[m]==' '), " " does not denote a space character but a string literal, you need ' ' instead..
  5. printf("%c", &str1[m]); is wrong, you want to print a char so you need str1[m] (without the &).
  6. You should remove both m++ and put that into the for statement: for (m = 1; m < len; m++), that makes the code clearer.

And possibly a few more problems.

And BTW your attempt doesn't remove the spaces from the string, it merely displays the string skipping spaces.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
1

There are a number of smaller errors here that are adding up.

First, check the bounds on your for loop. You're iterating from index 1 to index strlen(str1), inclusive. That's a reasonable thing to try, but remember that in C, string indices start from 0 and go up to strlen(str1), inclusive. How might you adjust the loop to handle this?

Second, take a look at this line:

if (&str1[m] == " ") {

Here, you're attempting to check whether the given character is a space character. However, this doesn't do what you think it does. The left-hand side of this expression, &str1[m], is the memory address of the mth character of the string str1. That should make you pause for a second, since if you want to compare the contents of memory at a given location, you shouldn't be looking at the address of that memory location. In fact, the true meaning of this line of code is "if the address of character m in the array is equal to the address of a string literal containing the empty string, then ...," which isn't what you want.

I suspect you may have started off by writing out this line first:

if (str1[m] == " ") {

This is closer to what you want, but still not quite right. Here, the left-hand side of the expression correctly says "the mth character of str1," and its type is char. The right-hand side, however, is a string literal. In C, there's a difference between a character (a single glyph) and a string (a sequence of characters), so this comparison isn't allowed. To fix this, change the line to read

if (str1[m] == ' ') {

Here, by using single quotes rather than double quotes, the right-hand side is treated as "the space character" rather than "a string containing a space." The types now match.

There are some other details about this code that need some cleanup. For instance, look at how you're printing out each character. Is that the right way to use printf with a character? Think about the if statement we discussed above and see if you can tinker with your code. Similarly, look at how you're reading a string. And there may be even more little issues here and there, but most of them are variations on these existing themes.

But I hope this helps get you in the right direction!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
0

For loop should start from 0 and less than length (not less or equal)

String compare is wrong. Should be char compare to ' ' , no &

Finding apace should not do anything, non space outputs. You ++m twice.

& on %c output is address not value

From memory, scanf stops on whitespace anyway so needs fgets

    int main()
{
    char str1[100];
    scanf("%s",str1);
    int len = strlen(str1);
    int m;
    for (m=0;m<len;++m){
        if (str1[m]!=' '){
            printf("%c",str1[m]);
        }

    }
    return 0;
}
LoztInSpace
  • 5,584
  • 1
  • 15
  • 27
0

There are few mistakes in your logic.

  • scanf terminates a string when it encounter any space or new line. Read more about it here. So use fgets as said by others.

  • & in C represents address. Since array is implemented as pointers in C, its not advised to use & while getting string from stdin. But scanf can be used like scanf("%s",str1) or scanf("%s",&str[1])

  • While incrementing your index put m++ inside else condition.

  • Array indexing in C starts from 0 not 1.

So after these changes code will becames something like

int main()
{
    char str1[100];
    fgets(str1, sizeof str1 , stdin);
    int len = strlen(str1);
    int m=0;
    while(m < len){
        if (str1[m] == ' '){
            m++;
        }
        else {
            printf("%c",str1[m]);
            m++;
        }

    }
    return 0;
}
svtag
  • 184
  • 4
  • 12