Removing dots in a string in C

寵の児 提交于 2019-12-08 14:17:47

问题


I'm making a little program in C where I would put in a couple of numbers and dots and then delete all the dots (.).

I was thinking about a whileloop but I cannot seem to quite understand what I should do next. So far I got this:

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

int main(int argc, char *argv[]) {

char s[30];
int k=0;
printf("Enter your account number including dots. \n");  
gets(s);
printf("Account number without dots:");
while (s[k]!=0) 
    {
       //?????
    }
return 0;

Am I on the right track or should I start differently and not use a while loop at all? I can only find solutions where there is a specific string that is not written by the user, but by the programmer...


回答1:


Put in an IF to only print characters that aren't a dot. Like the others suggested, you should probably change the gets to fgets as well.

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

int main(int argc, char *argv[]) {

    char s[30];
    int k=0;
    printf("Enter your account number including dots. \n");  
    gets(s);
    printf("Account number without dots:");
    while (s[k]!=0) {
        if ( s[k] != '.' ) {
            printf("%c", s[k]);
        }
        k++;
    }
    printf("\n");
    return 0;
}

With a while loop, I'm also worried that if the user puts in a full 30 characters, you won't reach your exit condition. To avoid this problem, a for loop would be better (since you already know the size of the array). However, if you do it this way, you'll also need to initialize your array "s" to be blank.

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

int main(int argc, char *argv[]) {

    char s[30];
    int k=0;
    printf("Enter your account number including dots. \n");  
    gets(s);
    printf("Account number without dots:");
    for ( k = 0 ; k < 30 ; k++ ) {
        if ( s[k] != '.' && s[k] != 0 ) {
            printf("%c", s[k]);
        }
        k++;
    }
    printf("\n");
    return 0;
}



回答2:


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

int main(int argc, char *argv[]) {

char s[30];
int k=0;
printf("Enter your account number including dots. \n");  
gets(s);
printf("Account number without dots:");
while (s[k]!=0) 
    {
       if(s[k] == '.')
          s[k] = s[k + 1];
       k++;
    }
    s[k] = '\0';
return 0;



回答3:


#include <stdio.h>

//remove the specified character from str
char *strrmc(char *str, char ch){
    char *from, *to;
    from = to = str;
    while(*from){
        if(*from == ch)
            ++from;
        else
            *to++ = *from++;
    }
    *to = '\0';
    return str;
}

int main(int argc, char *argv[]){
    char s[30] = "192.169.007";
    printf("%s\n", strrmc(s, '.'));//192169007
    return 0;
}



回答4:


Here's one way you might go at it - it's different from how you've started, but can easily be modified. It could be improved on as well, but we can quibble about that in further comments. :)

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

int main(int argc, char *argv[]) {
  /* Take account number in as argument to executable */

  int dotless_length = 30;
  char dotless[dotless_length];
  int k = 0;
  int i = 0;

  while (argv[1][k] != '\0' && i < dotless_length) {
    if (argv[1][k] >= 48 && argv[1][k] <= 57) { /* ascii decimal codes for 0-9 */
      dotless[i] = argv[1][k];
      i++;
    }
    else if (argv[1][k] != '.') {
      printf("invalid input:  %c\n", argv[1][k]);
      return 1;
    }
    k++;
  }

  dotless[i] = '\0'; /* null-terminate it! */

  printf("Account number without dots: %s\n", dotless);

  return 0;
}

Then compile with gcc -Wall -o zdotless filename.c and run with

./zdotless 401.863.3000 as an example.

Notes: This may look more harder since it goes into input sanitation (and cleanliness) a little more than your original - e.g.

  1. not assuming that user input consists solely of numbers and periods,
  2. saving the resulting dotless string (for presumable future manipulations?),
  3. having one place to change the length of dotless (a step towards not hardcoding it), and
  4. not being interactive.

When you call an executable, argv is what you've typed, so argv[0] is the executable name (./zdotless), argv[1] is the next argument (401.863.3000 as a string), and so on if there are more arguments. Since argv[1] is the string representation of your dotty input number, argv[1][0] is the first character of it, etc.

Since we're copying to dotless character-by-character rather than using string manipulation, you've got to tack on a null character manually. (That same null character is what you'd loop until reaching, when initially reading the input string.) Other questions?...



来源:https://stackoverflow.com/questions/20939225/removing-dots-in-a-string-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!