Best way to axe N bytes from the right?

say that i have strings that end in "text"

foo.9.text, bar.10.text, baz.11.text

and i want a C function to chop off the last four characters and replace each string with a '\0'; obviously with error-checking. Any ideas?

TIA!

Lots of ideas. Lots of questions.

  1. What arguments do you want to pass to your C function?
  2. What do you want your function to return?
  3. What errors do you want to detect?
  4. How do you want errors to be reported?
  5. What have you tried?

I want to pass the dunction strings that contain 4 alphabetics followed by a dot followed by an integer followed by a dot, and ending with the string "text".

Stripping off the bytes with a

       
while (*scp) {

        scp++;
        if (*scp == '.')
        {
           scp++;  // st the fitst "."
           break;
        }
}

gets me to the integer+"text"; what then? I want the function to return only the int. In other words, is there an easy way to get rid of the "text"?

Given

foo78.text

how to i return "78"? (i can use atoi() to turn the *chatacters* 78 into an int.

Is this a homework assignment?

There are a lot of inconsistencies in the statements in your last post and you didn't answer most of my questions. Let me try once more:

  1. What is the function prototype for your function?
  2. What do you want your function to return (an integer or a string)? Your example said it should return "78" (which is a string), but you said you could use atoi() which returns an int???
  3. If you want your function to return one value, why are you passing it more than one string?
  4. What errors do you want to detect?
  5. How do you want to report errors? (Special return values, changing errno, diagnostics printed to stderr, ...)
  6. Have you read the man page for atoi() ? If you have, why is the ".text" a concern?

the reason i didn't answer the questions last time is that it turns out that there were no [or few] errors to be checked for.

the prototype might be something like:

int returnInt(char *)

which would be called in a loop when the program has determined how many "xyz.NN.text" files there were. the "text" files are in each users ~/share/voice/" directory. each text file contains text of some kind. this text can be from one word such as "Hello" to possibly 50 words. (I have not tested the limit of the GTK+ 3.0 "label" widget.

the function should return the intereger vale of whatever string vale i can extract. foo.9.text would be stripped to the string "9" and one final line could easily return and int 9.

[!!] i just discovered that

 nptr = "89.text";

returns int 89. nothing in the man atoi page indicated that.

Nevertheless, if there is a way of [[easily]] stripping of the "text" i would be much obliged for some example code. ---or maybe find the src for atoi. Anyway, thanks for the clue.

PS: Homework!? I was part of the crew who turned v6 UNIX to v7 UNIX. I'm rusty but still at it ... .

You haven't said what system you're using, so I can't check your man pages, but on most systems, the atoi() man page will say that atoi(ptr) behaves like (int)strtol(str, (char **)NULL, 10) and the strtol() man page clearly states that it skips over leading whitespace and then (with the arguments specified) converts a string of digits with an optional leading + or - to an integer stopping at the 1st non-digit character it finds.

Try something like:

#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
int
returnInt(char *in) {
	char	*p;

	for(p = in; *p && !isdigit(*p); p++);
	return(atoi(p));
}

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

	for(i = 1; i < argc; i++)
		printf("returnInt(\"%s\") returned %d\n", argv, 
			returnInt(argv));
	return 0;
}

which when compiled and linked to produce a program named a.out and invoked as:

a.out abc.123.text def456xyz 789 abcdef ""

produces the output:

returnInt("abc.123.text") returned 123
returnInt("def456xyz") returned 456
returnInt("789") returned 789
returnInt("abcdef") returned 0
returnInt("") returned 0

Is this something like what you wanted to do?

You said there are no [or few] possible errors. Should it be an error if:

  1. there are no digits in the given input string?
  2. the string of digits found overflows the range of values that will fit in an int?
  3. there is no trailing ".text" ?
  4. there aren't at least two periods in your string?

Obviously, the code I provided last time doesn't perform any error checking. I will leave it as an exercise for the reader if any of these are important in the strings you'll be processing.

exactly so. thanks much!

---------- Post updated at 06:17 PM ---------- Previous update was at 03:32 PM ----------

the numbered text files are written by the GTK+ and the user who is typing and creating the files. the files are then read by the computer to others.
so the numbers, dots, and "text" ending are assured.

what i realized a few weeks ago was that i needed a Fourth button in case one of the users' sentences need to be repeated.