Home > Net >  How to get rid of first element in a string in c
How to get rid of first element in a string in c

Time:09-29

I am learning c and having difficulty on this problem. I am writing a function that getsa string containing a positive integer.

The function subtracts 1 from that integer and puts the obtained value in the string.

But my problem right now is that

for example When the input is "1000", I want the output to be "999". Not "0999". input "100" , I want the output to be "99", instead of "099". so I am going to just get rid of the first 0, at the end of this function. How can I get rid of the first element of a string??

I appreciate any feedback! thank you so much.

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

void str_subtract_one(char* num) {
  int len = 0;
  int i ;
  for (i=0; num[i]!='\0';i  ){
    len  ;


  }
  int j;
for (j = len;j !=0;j--){
  if (len != '0'){
    if (num[j-1]=='9'){
      num[j-1] = '8';
      break;
    }  
    else if (num[j-1]=='8'){
      num[j-1] = '7';
      break;
    }  
    else if (num[j-1]=='7'){
      num[j-1] = '6';
      break;
    }
    else if (num[j-1]=='6'){
      num[j-1] = '5';
      break;
    } 
    else if (num[j-1]=='5'){
      num[j-1] = '4';
      break;
    }
    else if (num[j-1]=='4'){
      num[j-1] = '3';  
      break;
    } 
    else if (num[j-1]=='3'){
      num[j-1] = '2';
      break;
    }  
    else if (num[j-1]=='2'){
      num[j-1] = '1';
      break;
    } 
    else if (num[j-1]=='1'){
      num[j-1] = '0';
      break;
    }
    else if (num[j-1]=='0'){
      num[j-1] = '9';
     
    }

  }
  
}    
  if (num[0] =='0')
  //I dont know how to get rid of the first 0 here.

   
 
}

int main()  {
  
  
  char nums[] = "10000";
  
  
  str_subtract_one(nums);

  printf("\nQ5\nstr_subtract_one function result; %s\n",nums);

  
  return 0;
}

CodePudding user response:

Probably you are not allowed to use the stdlib functions.

You need to move all the chars one character to the left:

    if (num[0] =='0')
    {
        for(size_t i = 1; i <= len; i  )
        {
            num[i - 1] = num[i];
        }
    }

Or if you are allowed to use some standard library functions:

    if (num[0] =='0')
    {
        memmove(num, num   1, len);
    }

https://godbolt.org/z/Prbzj6b9a

CodePudding user response:

#include <stdio.h>

void str_subtract_one(char* num) {    
    int number;
    sscanf(num, "%d", &number);
    number--;
    sprintf(num, "%d", number);
}

int main(void) {
  char num[5] = "1000";
  str_subtract_one(num);
  printf("%s\n", num);
  return 0;
}

CodePudding user response:

To get rid of the first character of the string just add 1 to the string pointer. Example:

puts("1234"   1);

prints:

234

Note that this answer literally addresses the question in the topic.

CodePudding user response:

A solution with minimal changes to your existing code:

char* str_subtract_one(char* num) {
  // as mentioned, this can be far more concise
  // ...

  // I dont know how to get rid of the first 0 here.

  // Advance the pointer past a leading zero.
  // If there's ever a case where there can be more than one leading '0',
  // you'll need to handle that here in a loop instead
  if (num[0] =='0')
  {
    num  ;
  }

  return num;
}
 
int main()  {
  char nums[] = "10000";
  char* newNum = str_subtract_one(nums);

  // prints 9999
  printf("\nQ5\nstr_subtract_one function result; %s\n",newNum);

  return 0;
}

example

  • Related