What this code does simply is to break up a sentence into individual word, for example: you input My name is John, it returns:
My
name
is
John
I'll like to know if there's any better way to rewrite this?
int main() {
int w_size = 0;
bool check_bool = false;
char l_str[81];
char *ptr_to_word[81];
for (char *res_p = &(l_str[0]); *res_p != '\0'; res_p++) {
if ((*res_p != '.') && (*res_p != ',') && (*res_p != ' ') && (check_bool == false)) {
ptr_to_word[w_size] = res_p;
w_size++;
check_bool = true;
}
if (((*res_p == '.') || (*res_p == ',') || (*res_p == ' ')) && (check_bool == true)) {
check_bool = false;
}
}
if (w_size == 0) {
printf("no solution");
} else {
for (int i = 0; i < w_size; i++) {
char *a = ptr_to_word[i];
while ((*a != ',') && (*a != '.') && (*a != '\0') && (*a != ' ')) {
printf("%c", *a);
a++;
}
printf("\n");
}
}
return 0;
}
the following proposed code:
prompts the user for the sentence to be divided into words
cleanly compiles
performs the desired functionality
And now, the proposed code: (EDIT per chqrlie)
#include <stdio.h>
#include <string.h>
#define MAX_BUF_LEN 1024
#define MAX_WORDS 100
int main( void )
{
char buffer[ MAX_BUF_LEN ] = {0};
char *words[ MAX_WORDS ] = {NULL};
printf( "%s\n", "Please enter a sentence to be divided into words" );
if( fgets( buffer, sizeof( buffer ), stdin ) )
{
size_t wordCount = 0;
char *token;
token = strtok( buffer, ",. " );
while( wordCount < MAX_WORDS && token )
{
words[ wordCount ] = token;
wordCount++;
token = strtok( NULL, ",. " );
}
for( size_t i = 0; i < wordCount; i++ )
{
printf( "%zu: %s\n\n", i+1, words[i] );
}
}
}
Here is the results of a typical run of the proposed code:
Please enter a sentence to be divided into words
This is a sentence to be divided into words
1: This
2: is
3: a
4: sentence
5: to
6: be
7: divided
8: into
9: words
If you dont need to store the words into an array, you can output them directly:
#include <stdio.h>
#include <string.h>
int main() {
char str[81];
printf("Enter string: ");
if (fgets(str, sizeof str, stdin)) {
int pos = 0, len, index = 1;
for (;;) {
/* skip initial separators */
pos += strspn(str + pos, ",.\n ");
if (str[pos] == '\0')
break;
/* compute the length of the word */
len = strcspn(str + pos, ",.\n ");
printf("%d: %.*s\n", index++, len, str + pos);
pos += len;
}
}
return 0;
}
Related
I have this assignment:
Replace the numbered word in the sentence with three characters: ???
The problem is I can't replace the whole word by its number, my program changes only 3 first characters.
Here is my code:
#include <stdio.h>
#include <string.h>
int main() {
char sentence[100];
int word_number;
int pos;
int i;
printf("Enter sentence: ");
fgets(sentence, 100, stdin);
printf("Enter word number: ");
scanf("%d", &word_number);
pos = 0;
for (i = 0; i < strlen(sentence); i++) {
if (sentence[i] == ' ') {
word_number--;
}
if (word_number == 1) {
pos = i;
break;
}
}
// Update the sentence
sentence[pos] = '?';
sentence[pos + 1] = '?';
sentence[pos + 2] = '?';
printf("Result sentence: %s\n", sentence);
return 0;
}
There are multiple issues in the code:
Your approach only works if the word to be replaced has exactly 3 letters. For longer or shorter words, you need to move the contents of the remainder of the string.
Another restriction in your code is you assume that words a separated by a single space, which may of may not be explicitly stated in the assignment.
Similarly, you assume the n-th word is followed by a space, which may not be the case if the word is the last one in the sentence.
you should check the return values of fgets() and scanf() to detect invalid or missing input.
Here is a modified version.
#include <stdio.h>
#include <string.h>
int is_sep(char cc) {
// separators are space, newline and the null byte
return (cc == ' ' || cc == '\n' || cc == '\0');
// you could also accept punctuation as word separators:
//return strchr(" \t\n.,:;?!'`"##$%^&*()[]{}<>/`~+=|", cc);
}
int main() {
char sentence[200];
int word_number;
printf("Enter sentence: ");
// read a sentence, allow for 2 extra bytes to insert replacement
if (!fgets(sentence, sizeof(sentence) - 2, stdin))
return 1;
printf("Enter word number: ");
if (scanf("%d", &word_number) != 1)
return 1;
int len = strlen(sentence);
int start = 0;
int wn = 1;
char last = ' ';
for (int i = 0;; i++) {
char cc = sentence[i];
if (is_sep(cc)) {
if (!is_sep(last)) {
/* end of word */
if (wn == word_number) {
// replace this word with `???`
memmove(sentence + start + 3, sentence + i, len + 1 - i);
memcpy(sentence + start, "???", 3);
break;
}
wn++;
}
} else {
if (is_sep(last)) {
/* start of a word */
start = i;
}
}
last = cc;
if (cc == '\0')
break;
}
printf("Result sentence: %s\n", sentence);
return 0;
}
I’m not sure I’d try to do this in-place. For small data, it doesn’t hurt to just create a new buffer, even if it is just a temporary.
But, since we are posting solutions, here’s a single-pass version:
#include <iso646.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void replace_nth_word_with( char * dest, int n, const char * src, const char * word )
{
char last = ' ';
while (*src) // For each character in src:
if ((*src != ' ') and (last == ' ') and !n--) // If we found a word-begin AND it is the Nth word:
{ //
for (size_t k = 0; word[k]; k++) // Copy the new word to dest
*dest++ = word[k]; //
while (*src and (*src != ' ')) ++src; // Skip the old word in src
} //
else // Otherwise just copy the character over
last = *dest++ = *src++; //
*dest = '\0'; // End of string
}
void ask_string( const char * prompt, char * s, size_t n )
{
printf( "%s", prompt );
fgets( s, (int)n, stdin );
char * p = strpbrk( s, "\n\r" );
if (p) *p = '\0';
}
void ask_int( const char * prompt, int * n )
{
char s[20];
printf( "%s", prompt );
fgets( s, sizeof s, stdin );
*n = strtol( s, NULL, 10 );
// It hurts to write this without error checking, LOL
}
int main(void)
{
char sentence[1000];
ask_string( "Sentence? ", sentence, sizeof sentence );
int word_number;
ask_int( "Word number (counting from 1)? ", &word_number );
char new_word[100];
ask_string( "New word? ", new_word, sizeof new_word );
char new_sentence[sizeof sentence + sizeof new_word];
replace_nth_word_with( new_sentence, word_number-1, sentence, new_word );
printf( "\"%s\"\n", new_sentence );
return 0;
}
Printing the digits present in the string.
Suppose i have an input like this
Input: {1,32,33,41,59}
The output should look like this
Output: 1 32 33 41 59
My code is
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char input[200],words[10][10];
int length=0,i=0,j=0,k=0,t;
fgets(input,200,stdin);
//counting the length of str
while(input[length] != '\0')
{
length++;
}
for(i=1;i<=length;i++)
{
if(input[i] == ',' || input[i] == '}')
{
words[j][k] = '\0';
j++;
k=0;
}
else
{
words[j][k] = input[i];
k++;
}
}
int temp[j];
for(i=0;i<j-1;i++)
{
temp[i] = atoi(words[i]);
printf("%d\n",temp[i]);
}
return 0;
}
My code just prints the first digit in the string. I can't figure out why.
Use j instead of j-1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char input[200],words[10][10];
int length=0,i=0,j=0,k=0,t;
fgets(input,200,stdin);
//counting the length of str
while(input[length] != '\0')
{
length++;
}
for(i=1;i<=length;i++)
{
if(input[i] == ',' || input[i] == '}')
{
words[j][k] = '\0';
j++;
k=0;
}
else
{
words[j][k] = input[i];
k++;
}
}
int temp[j];
for(i=0;i<j;i++)
{
temp[i] = atoi(words[i]);
printf("%d\n",temp[i]);
}
return 0;
}
I made a few edits to your code and believe I got it working the way you want. I commented the changes, please look below.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char input[200],words[10][10];
int length=0,i=0,j=0,k=0,t;
fgets(input,200,stdin);
while(input[length] != '\0')
{
length++;
}
for(i=1;i<=length;i++)
{
if(input[i] == ',' || input[i] == '}')
{
words[j][k] = '\0';
j++;
k=0;
}
else
{
words[j][k] = input[i];
k++;
}
}
int temp[j];
//Iterate through all elements in the array
//0 ---> j-1 is j elements
for(i=0;i < j ;i++)
{
temp[i] = atoi(words[i]);
//print on the same line
printf("%d ",temp[i]);
}
//newline here
printf("\n");
return 0;
}
Your approach is too complicated and as such has bugs.
There is no need to define a character two-dimensional array to output stored in a string numbers. It is enough from the very beginning to use a standard function like for example strtoull.
Here you are.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void)
{
enum { N = 200 };
char input[N];
while ( 1 )
{
printf( "Input: " );
if ( !fgets( input, sizeof( input ), stdin ) || input[0] == '\n') break;
for ( char *p = input; *p; )
{
if ( isdigit( ( unsigned char )*p ) )
{
char *endptr;
unsigned long long int num = strtoull( p, &endptr, 10 );
printf( "%llu\n", num );
p = endptr;
}
else
{
++p;
}
}
}
return 0;
}
The program output might look like
Input: {1,32,33,41,59}
1
32
33
41
59
Input:
I suggest you this:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
const char *const s = ",";
int main()
{
char input[200], *ptri, *ptr;
fgets(input, 200, stdin);
ptr = input;
while (*ptr != '{' && *ptr != '\0')
ptr++;
if (*ptr == '\0') {
return -1; /* not found */
}
ptr++;
ptri = ptr;
while (*ptr != '}' && *ptr != '\0')
ptr++;
if (*ptr == '\0') {
return -1; /* not found */
}
*ptr = '\0';
ptr = strtok(ptri, s);
while (ptr != NULL) {
printf("%s ", ptr);
ptr = strtok(NULL, s);
}
printf("\n");
return 0;
}
I'm trying to split a sentence the user inputs to an array of words so I can later manipulate the words separately as strings.
The code is compiling but prints only garbage after the user input.
I tried debugging but don't see the problem. Can someone help me fix it?
#include <stdio.h>
#include <string.h>
int main() {
char str[1000];
int i = 0;
char rev[1000][1000];
int r = 0;
puts("Enter text:");
gets(str);
int k, length = 0;
printf_s("So the words are:\n");
while (str[i] != '\0') {
if (str[i] == ' ') {
k = i - length;
do {
rev[r][k] = (str[k]);
k++;
} while (str[k] != ' ');
printf(" ");
length = (-1);
r++;
} else
if (str[i + 1] == '\0') {
k = i - length;
do {
rev[r][k] = (str[k]);
k++;
} while (str[k] != '\0');
length = 0;
r++;
}
length++;
i++;
}
for (int r = 0; r < 1000; r++)
printf("%s ", rev[r]);
return 0;
}
fix like this
#include <stdio.h>
int main(void) {
char str[1000];
char rev[1000][1000];
puts("Enter text:");
fgets(str, sizeof str, stdin);//Use fgets instead of gets. It has already been abolished.
int r = 0;
int k = 0;
for(int i = 0; str[i] != '\0'; ++i){
if (str[i] == ' ' || str[i] == '\n'){//is delimiter
if(k != 0){
rev[r++][k] = '\0';//add null-terminator and increment rows
k = 0;//reset store position
}
} else {
rev[r][k++] = str[i];
}
}
if(k != 0)//Lastly there was no delimiter
rev[r++][k] = '\0';
puts("So the words are:");
for (int i = 0; i < r; i++){
printf("%s", rev[i]);
if(i < r - 2)
printf(", ");
else if(i == r - 2)
printf(" and ");
}
return 0;
}
Replace you declaration
char rev[1000][1000];
with
char * rev[1000]; // We will need pointers only
int i = 0; // Index to previous array
and all your code after
puts( "Enter text:" );
with this:
fgets( str, 998, stdin ); // Safe way; don't use gets(str)
const char delim[] = ",; "; // Possible delimiters - comma, semicolon, space
char *word;
/* Get the first word */
word = strtok( str, delim );
rev[i++] = word;
/* Get the next words */
while( word != NULL )
{
word = strtok( NULL, delim );
rev[i++] = word;
}
/* Testing */
for (int r = 0; r < i - 1; r++)
printf( "%s\n", rev[r] );
return 0
}
As you can see, all dirty work is done with the strtok() function ("string to tokens") which walks through other and other words ("tokens"), recognizing them as delimited by one or more characters from the string delim.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int count_spaces(char *str)
{
if (str == NULL || strlen(str) <= 0)
return (0);
int i = 0, count = 0;
while (str[i])
{
if (str[i] == ' ')
count++;
i++;
}
return (count);
}
int count_char_from_pos(char *str, int pos)
{
if (str == NULL || strlen(str) <= 0)
return 0;
int i = pos, count = 0;
while (str[i] && str[i] != ' ')
{
count++;
i++;
}
return count;
}
char **get_words(char *str)
{
if (str == NULL || strlen(str) <= 0)
{
printf("Bad string inputed");
return NULL;
}
int i = 0, j = 0, k = 0;
char **dest;
if ((dest = malloc(sizeof(char*) * (count_spaces(str) + 1))) == NULL
|| (dest[0] = malloc(sizeof(char) * (count_char_from_pos(str, 0) + 1))) == NULL)
{
printf("Malloc failed\n");
return NULL;
}
while (str[i])
{
if (str[i] == ' ') {
dest[j++][k] = '\0';
if ((dest[j] = malloc(sizeof(char) * (count_char_from_pos(str, i) + 1))) == NULL)
{
printf("Malloc failed\n");
return NULL;
}
k = 0;
}
else {
dest[j][k++] = str[i];
}
i++;
}
dest[j][k] = 0;
dest[j + 1] = NULL;
return dest;
}
int main(void) {
char *line = NULL;
size_t n = 0;
getline(&line, &n, stdin);
printf("%s\n", line);
line[strlen(line) - 1] = 0;
printf("%s\n", line);
char **tab = get_words(line);
int i = 0;
while (tab[i])
{
printf("%s\n", tab[i++]);
}
}
here is a long but fully working example
get the user input
then send it to get_words function. It will get the number of words, the number of characters for each words, allocate everything in memory and writes chars then return it. You get a char ** and prints it just tested it it works
If you wish to split a string into an array of strings, you should consider the strtok function from #include <string.h>. The strtok function will the split the string on the given delimiter(s). For your case, it would the " ".
Using the strtok example from Tutorials Point:
#include <string.h>
#include <stdio.h>
int main(){
char str[80] = "This is - www.tutorialspoint.com - website";//The string you wish to split
const char s[] = "-";//The thing you want it to split from. But there is no need to this.
char *token;//Storing the string
/* get the first token */
token = strtok(str, s);//Split str one time using the delimiter s
/* walk through other tokens */
while( token != NULL )
{
printf( " %s\n", token );//Print the string
token = strtok(NULL, s);//Split the string again using the delimiter
}
return(0);
}
I have problem with my alignement. This time I want my program to return words that ends and starts with the same letter. I've wrote something like this, but it seems to return random words.
#include <stdio.h>
#include <string.h>
void main()
{
char str[100];
int i, t, j, len;
printf("Enter a string : ");
scanf("%[^\n]s", str);
len = strlen(str);
str[len] = ' ';
for (t = 0, i = 0; i < strlen(str); i++)
{
if ((str[i] == ' ') && (str[i - 1] == str[0]))
{
for (j = t; j < i; j++)
printf("%c", str[j]);
t = i + 1;
printf("\n");
}
else
{
if (str[i] == ' ')
{
t = i + 1;
}
}
}
}
You can use strtok to split the strings from stdin, then apply a letter checker on each parsed word one at a time.
Something like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXCHAR 100
int is_start_end(char *word);
void exit_if_null(void *ptr, const char *msg);
int
main(void) {
char str[MAXCHAR];
char *word;
char **all_words;
int words_size = 1, word_count = 0;
int i, found;
all_words = malloc(words_size * sizeof(*all_words));
exit_if_null(all_words, "initial Allocation");
printf("Enter words(enter empty line to terminate):\n");
while (fgets(str, MAXCHAR, stdin) != NULL && strlen(str) != 1) {
word = strtok(str, " \n");
while (word !=NULL) {
if (words_size == word_count) {
words_size *= 2;
all_words = realloc(all_words, words_size * sizeof(*all_words));
exit_if_null(all_words, "Reallocation");
}
all_words[word_count] = malloc(strlen(word)+1);
exit_if_null(all_words[word_count], "Initial Allocation");
strcpy(all_words[word_count], word);
word_count++;
word = strtok(NULL, " \n");
}
}
printf("Words that have equal first and last letters:\n");
found = 0;
for (i = 0; i < word_count; i++) {
if (is_start_end(all_words[i])) {
found = 1;
printf("%s\n", all_words[i]);
}
free(all_words[i]);
all_words[i] = NULL;
}
if (found == 0) {
printf("None Found\n");
}
free(all_words);
all_words = NULL;
return 0;
}
int
is_start_end(char *word) {
int len;
len = strlen(word);
if ((len == 1) || (tolower(word[0]) == tolower(word[len-1]))) {
return 1;
}
return 0;
}
void
exit_if_null(void *ptr, const char *msg) {
if (!ptr) {
printf("Unexpected null pointer: %s\n", msg);
exit(EXIT_FAILURE);
}
}
This line removes the null terminator of the string:
len = strlen(str);
str[len] = ' ';
thus the string no longer exists, what is left is just an ordinary array of characters.
The next call to strlen, in the body of the for loop, will cause undefined behavior.
I am new to programming in C, and I'm working on a simple program to take the user input (a basic phone number, ie: (678)-653.7539), and will output it in standard format).
The approach I took was first taking out all periods, hyphens, and parenthesis.
Currently the program prints out just numbers, however the format I want is:
(xxx) xxx-xxxx
I'm thinking creating a method with an array, and then iterating through (similar to stack?) having it input "(" before i[0] and again after i[2], and so on.
Is this the right approach?
#include <stdio.h>
void removeHyphen( char s[], char x );
void removeLeftParen( char s[], char f );
void removeRightParen( char s[], char g );
void removePeriod( char s[], char h );
int main()
{
char s[50];
printf("Enter your phone number:\n");
scanf("%s", s);
printf( "Your phone number: %.13s\n", s );
removeHyphen( s, '-' );
removeLeftParen(s, '(');
removeRightParen(s, ')');
removePeriod(s, '.');
printf( "Formatted phone number: %.10s\n", s );
getchar();
return 0;
}
void removeHyphen(char s[], char x)
{
int i, j;
for (i = 0 ; s[i] != 0 ; ++i)
{
while(s[i]==x)
{
j=i;
while(s[j]!=0)
{
s[j]=s[j+1];
++j;
}
}
}
}
void removeLeftParen(char s[], char f)
{
int i, j;
for (i = 0 ; s[i] != 0 ; ++i)
{
while(s[i]==f)
{
j=i;
while(s[j]!=0)
{
s[j]=s[j+1];
++j;
}
}
}
}
void removeRightParen(char s[], char g)
{
int i, j;
for (i = 0 ; s[i] != 0 ; ++i)
{
while(s[i]==g)
{
j=i;
while(s[j]!=0)
{
s[j]=s[j+1];
++j;
}
}
}
}
void removePeriod(char s[], char h)
{
int i, j;
for (i = 0 ; s[i] != 0 ; ++i)
{
while(s[i]==h)
{
j=i;
while(s[j]!=0)
{
s[j]=s[j+1];
++j;
}
}
}
}
You know exactly what your end product should look like. It'll be char result[15]. So a simple brute force algorithm would look like:
//set the known characters in the output string
result[ 0 ] = '(';
result[ 4 ] = ')';
result[ 5 ] = ' ';
result[ 9 ] = '-';
result[ 14 ] = '/0'; //null terminator
int index = 0;
//pseudocode
foreach( character in input )
if character is a number
if index == 0, 4, 5, 9
++index;
if index == 14 //we're out of room
print result;
exit;
result[ index++ ] = character;
else
ignore character
Where "character is a number" would probably be the only function you'd need to write.
You may not need all the remove logic. You may just iterate over the input and copy the numeric characters.
Pseudo-code idea:
char output[50]; // better: char output[sizeof input];
// This is essentially processed/normalized input.
// In fact, since we know that it is a 10-digit
// phone number we can just do: char output[10];
// If you ever need to store the phone number for
// long term, the last option may be the best option.
const int n = actual length of input, e.g. strlen()
int j = 0;
for (int i = 0; i < n; ++i) {
if (isdigit((unsigned char) input[i]) {
output[j++] = input[i];
}
}
// Validate 'output', for e.g. check that it has 10 characters
// Print output in desired format
See manual page for isdigit().
A different program structure employing the same idea is the following. While accepting input, scan them as characters and ignore the non-digit characters.
I suggest the use of strtok.
The following is an example
#include <stdio.h>
#include <string.h>
int main(void){
char s[50], f[50];
char *part[3] = { NULL };
char *p;
int i;
printf("Enter your phone number:\n");
scanf("%49s", s);
printf( "Your phone number: %s\n", s );
p = strtok(s, "-().");
for(i=0; p!=NULL && i<3; ++i){
part[i] = p;//Better to add one of the check is made up of numbers.
p = strtok(NULL, "-().");
}
if(i==3){
sprintf(f, "(%s) %s-%s", part[0], part[1], part[2]);
printf( "Formatted phone number: %s\n", f );
} else {
printf("invalid format\n");
}
getchar();
return 0;
}
After you have removed all the unwanted characters you can do this
void printFormatted(char *s)
{
int i;
if (s == NULL)
return;
fputc('(', stdout);
for (i = 0 ; ((i < 3) && (s[i] != '\0')) ; ++i)
fputc(s[i], stdout);
fputc(')', stdout);
fputc(' ', stdout);
if (s[i] == '\0')
return;
for ( ; ((i < 6) && (s[i] != '\0')) ; ++i)
fputc(s[i], stdout);
fputc('-', stdout);
if (s[i] == '\0')
return;
for ( ; s[i] != '\0' ; ++i)
fputc(s[i], stdout);
fputc('\n', stdout);
}
Although you don't really need to remove anything if you are just interested in the output of the program, you could use this
#include <stdio.h>
#include <ctype.h>
void printFormatted(char *phone);
int main()
{
char phone[50];
printf("Enter your phone number: ");
if (scanf("%49s%*c", phone) == 1)
{
printf( "Your input : %s\n", phone);
printf("Formatted phone number : ");
printFormatted(phone);
printf("\n");
}
return 0;
}
int putdigit(char digit)
{
/* Print a charater if it's a digit (0-9) */
if (isdigit((int)digit) == 0)
return 0;
fputc(digit, stdout);
return 1;
}
void printFormatted(char *phone)
{
int i;
int j;
/* Always be safe */
if (phone == NULL)
return;
/* Output the `(' */
fputc('(', stdout);
/* Output 3 digits */
for (i = 0, j = 0 ; ((j < 3) && (phone[i] != '\0')) ; ++i)
j += putdigit(phone[i]);
/* Output the `)' and a space */
fputc(')', stdout);
fputc(' ', stdout);
/* Check if there are more characters */
if (phone[i] == '\0')
return;
/* Output 3 digits */
for (j = 0 ; ((j < 3) && (phone[i] != '\0')) ; ++i)
j += putdigit(phone[i]);
/* Output the hypen */
fputc('-', stdout);
/* Check if there are more characters */
if (phone[i] == '\0')
return;
/* Output the rest of the characters */
for ( ; phone[i] != '\0' ; ++i)
putdigit(phone[i]);
fputc('\n', stdout);
}
Another approach. Build the string per an interpreted format.
#include <ctype.h>
// 0: success, 1 fail
int FormatPhoneNumber(const char *format, char *dest, const char *src) {
int i;
for (i = 0; format[i]; i++) {
if (format[i] == 'x') {
while (!isdigit((unsigned char) *src)) {
if (*src++ == '\0') {
dest[i] = '\0';
return 1; // fail, missing digit
}
}
dest[i] = *src++;
} else {
dest[i] = format[i];
}
}
dest[i] = '\0';
while (*src && !isdigit((unsigned char) *src)) src++;
return *src ? 1 : 0;
}
#include <stdio.h>
int main(void) {
const char format[] = "(xxx) xxx-xxxx";
char buf[sizeof format];
int result = FormatPhoneNumber(format, buf, " (678)-653.7539),");
printf("%d '%s'\n", result, buf);
result = FormatPhoneNumber(format, buf, "Jenny: 111-867-5309");
printf("%d '%s'\n", result, buf);
return 0;
}
0 '(678) 653-7539'
0 '(111) 867-5309'