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

int main(){
    char array[]="Arijit Saha Student";
    spaceremover(array);
    getch();
    return 1;
}

int spaceremover(char a[]){
    int i;
   // printf("L=%d",strlen(a));
    char final[strlen(a)+1];
    int count=0;
    for(i=0;i<strlen(a);i++)
    {
        if(a[i]!=' ')
            final[count]=a[i];
        count++;
    }
    final[count]='\0';
    int j=0;
    for(j=0;j<strlen(final);j++)
    printf("%c",final[j]);
    // printf("\n%s",final);
    return 1;
}

With this example code the output is Arijit.Saha, but my desired output is ArijitSahaStudent. Why am I getting the wrong output? Where the . is coming from?

6
  • 1
    This probably belongs on CodeReview Commented Mar 4, 2013 at 16:28
  • @VladLazarenko Really? I thought SO is for programming issues and CR is for reviewing code. Then what kind of questions belong here? Commented Mar 4, 2013 at 16:30
  • 2
    The '.' is probably just junk data leftover from something else. Your biggest problem is that count is being incremented no matter what. You should put curly braces around final[count]=a[i]; and count++ so count only increments when a letter is added to the array. Currently, when it gets to a space, nothing is added to the final array at that position Commented Mar 4, 2013 at 16:30
  • 1
    char final[strlen(a)+1]; : is this legal in C? Commented Mar 4, 2013 at 16:31
  • 2
    @anishsane It is with C99 compliant compilers I believe. Commented Mar 4, 2013 at 16:32

4 Answers 4

6

The error is here:

if(a[i]!=' ')
    final[count]=a[i];
count++;

The count++ should be included in the if, so:

if(a[i]!=' ') {
    final[count]=a[i];
    count++;
}

Note: It is considered good programming practice to always use curly braces after if statements.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 nice spotted. It just shows how important is the proper indentation of the code.
2

In the for loop you increase count even if a[i] == ' ' .
So when a[i] == ' ' you only increase count but doesn't set final[count] to anything.

I assume you ment to write:

if (a[i] != ' ')
{
    final[count] = a[i];
    count++;
}

Comments

0

You are not incrementing count correctly. It needs to be in the if loop. This might be easier for you to see if things are indented correctly.

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

int main(){
  char array[]="Arijit Saha Student";
  spaceremover(array);
  return 1;
}

int spaceremover(char a[]){
  int i;
  // printf("L=%d",strlen(a));
  char final[strlen(a)+1];
  int count=0;
  for(i=0;i<strlen(a);i++)
  {
    if(a[i]!=' ')
      final[count++]=a[i];
  }
  final[count++]='\0';
  int j=0;
  for(j=0;j<strlen(final);j++)
    printf("%c",final[j]);
  // printf("\n%s",final);
  return 1;
}

Comments

0

In the loop, the count should increase if a[i] not equal to space

if(a[i]!=' ') final[count]=a[i]; count++;

if(a[i]!=' ')
final[count++]=a[i];
//count++;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.