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,924 questions

51,857 answers

573 users

How to remove the second digit from a number in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
unsigned int remove_second_digit(unsigned int n) {
    static char tmp[10];
     
    sprintf(tmp, "%d", n);

    int index_from = 1;
    int index_to = 2;
    int sectionlen = index_to - index_from;
    int stringlen = strlen(tmp);

    memmove(tmp + index_from, tmp + index_from + sectionlen, stringlen - index_to);
    tmp[stringlen - sectionlen] = '\0';

    return atoi(tmp);
}
 
int main(void) {
    unsigned int n = 8405796;
    printf("n: %d\n", n);
 
    n = remove_second_digit(n);
     
    printf("n: %d\n", n);
     
    return 0;
}
 
 
   
/*
run:
   
n: 8405796
n: 805796

*/

 

 



answered Jan 12, 2024 by avibootz
edited Jan 12, 2024 by avibootz

Related questions

1 answer 100 views
1 answer 86 views
1 answer 120 views
1 answer 96 views
1 answer 120 views
1 answer 95 views
1 answer 89 views
...