Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,933 questions

51,870 answers

573 users

How to delete the N digit of a number in C

2 Answers

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

void substring(char s[], char sub[], int pos, int len) {
   int i = 0;
     
   while (i < len) {
      sub[i] = s[pos + i - 1];
      i++;
   }
   sub[i] = '\0';
}

int main(void) {
	int number = 873152, N = 4;
	char s[10];
 
    sprintf(s, "%d", number); 
	
	char beforeN[10];
	substring(s, beforeN, 1, N - 1);
    
	char afterN[10];
	substring(s, afterN, N + 1, strlen(s) - N);

	number = atoi(strcat(beforeN, afterN));
	
	printf("%i\n", number);
}




/*
run:
  
87352
  
*/

 



answered Jun 4, 2020 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main(void) {
    int number = 873152, N = 4;
    char s[10];
  
    sprintf(s, "%d", number); 
     
    char beforeN[10] = "\0";
    strncpy(beforeN, s, N - 1);
     
    char afterN[10] = "\0";
	strncpy(afterN, s + N, strlen(s) - N);
 
    number = atoi(strcat(beforeN, afterN));
     
    printf("%i\n", number);
}
 
 
 
 
/*
run:
   
87352
   
*/

 



answered Jun 4, 2020 by avibootz

Related questions

1 answer 114 views
2 answers 226 views
1 answer 137 views
1 answer 132 views
1 answer 130 views
1 answer 119 views
1 answer 105 views
...