Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 651925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:12:54+00:00 2026-05-13T22:12:54+00:00

Quick question, What have I done wrong here. The purpose of this code is

  • 0

Quick question, What have I done wrong here. The purpose of this code is to get the input into a string, the input being “12 34”, with a space in between the “12” and “32” and to convert and print the two separate numbers from an integer variable known as number. Why doesn’t the second call to the function copyTemp, not produce the value 34?. I have an index_counter variable which keeps track of the string index and its meant to skip the ‘space’ character?? what have i done wrong?

thanks.

#include <stdio.h>
#include <string.h>
int index_counter = 0;
int number;
void copyTemp(char *expr,char *temp);

int main(){
 char exprstn[80]; //as global?
 char tempstr[80];

 gets(exprstn);
 copyTemp(exprstn,tempstr);
 printf("Expression: %s\n",exprstn);
 printf("Temporary: %s\n",tempstr);
 printf("number is: %d\n",number);
 copyTemp(exprstn,tempstr);      //second call produces same output shouldnt it now produce 34 in the variable number?
 printf("Expression: %s\n",exprstn);
 printf("Temporary: %s\n",tempstr);
 printf("number is: %d\n",number);

 return 0;
}
void copyTemp(char *expr,char *temp){
 int i;
 for(i = index_counter; expr[i] != '\0'; i++){
  if (expr[i] == '0'){
   temp[i] = expr[i];
  }
  if (expr[i] == '1'){
   temp[i] = expr[i];
  }
  if (expr[i] == '2'){
   temp[i] = expr[i];
  }
  if (expr[i] == '3'){
   temp[i] = expr[i];
  }
  if (expr[i] == '4'){
   temp[i] = expr[i];
  }
  if (expr[i] == '5'){
   temp[i] = expr[i];
  }
  if (expr[i] == '6'){
   temp[i] = expr[i];
  }
  if (expr[i] == '7'){
   temp[i] = expr[i];
  }
  if (expr[i] == '8'){
   temp[i] = expr[i];
  }
  if (expr[i] == '9'){
   temp[i] = expr[i];
  }
  if (expr[i] == ' '){ 
   temp[i] = '\0';
   sscanf(temp,"%d",&number); 
   index_counter = i+1; //skips?
  }
 }
 // is this included here? temp[i] = '\0'; 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:12:55+00:00Added an answer on May 13, 2026 at 10:12 pm

    There are a few problems in your program:

    • You are using the same index into
      expr and temp arrays. This works for
      the first time since both will be 0
      to start with but when you want to
      process the 2nd number, you need to
      reset the index into the temp array
      back to 0. Clearly this cannot be
      done using a single index. You’ll
      have to use two indices, i and j.
    • By the time you complete the
      processing of the 2nd number ( 34 in
      "12 34") you’ll reach the end of the
      string and hence the sscanf never
      gets run on the second occasion ( in
      general for the last occasion). So
      after the for loop you need another
      sscanf to extract the last number. Also you should return from the function once you’ve extracted the number from the string and incremented i.
    • You should avoid using gets() and use
      fgets() instead because of security
      reasons.
    • You can combine the multiple test for
      the digits into a single test as
      shown:

    Something like this.

    void copyTemp(char *expr,char *temp){
        int i;
        int j = 0;
        for(i = index_counter; expr[i] != '\0'; i++){
    
            if (expr[i] >= '0' && expr[i]<='9'){
                temp[j++] = expr[i]; // copy the digit into temp..increment j.
            }    
            else if (expr[i] == ' '){ // space found..time to extract number.
                temp[j] = '\0'; // terminate the temp.
                sscanf(temp,"%d",&number); // extract.
                index_counter = i+1; // skip the space.
                        return; // done converting...return..must not continue.
            }
        }
        // have reached the end of the input string..and still need to extract a 
        // the last number from temp string.
        temp[j] = '\0';
        sscanf(temp,"%d",&number);
    }
    

    After these changes it works as expected:

    $ gcc b.c 2> /dev/null && ./a.out
    12 34
    Expression: 12 34
    Temporary: 12
    number is: 12
    Expression: 12 34
    Temporary: 34
    number is: 34
    

    Your approach is very fragile…if a user gives multiple spaces between the input numbers..your program will fail.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 397k
  • Answers 397k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Every HTTP method: POST, GET, PUT, DELETE, etc. caries its… May 15, 2026 at 3:25 am
  • Editorial Team
    Editorial Team added an answer No, it can't. If you need to interact with javascript,… May 15, 2026 at 3:25 am
  • Editorial Team
    Editorial Team added an answer You don't need recursive parent/child here, but just Many-To-Many relationship:… May 15, 2026 at 3:25 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.