# Concatenation of strings in C

• (2 Pages)
• 1
• 2

## 18 Replies - 1842 Views - Last Post: 14 October 2012 - 09:58 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=295420&amp;s=720a504f85343080608e5637cef5ea39&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 CoryMore

Reputation: 6
• Posts: 111
• Joined: 26-June 12

# Concatenation of strings in C

Posted 12 October 2012 - 09:18 PM

I have to do the following:

Quote

Forms an integer value by taking the Nth digits of Value, starting
at the low digit, and concatenating those digits. *
Examples:
everyNth(12345, 1) = 12345
everyNth(12345, 2) = 135
everyNth(12345, 3) = 25
everyNth(12345, 4) = 15
everyNth(12345, 5) = 5 *
Pre: Value and N have been initialized and N is greater than zero.
Post:
Returns: integer value formed by concatenating the specified digits of Value; if N = 0, the return value is undefined *
Restrictions:
1. You may use any integer operations supported in C.
2. You may also use any selection (if, if..else, etc) or iteration (for, while) constructs.
3. You may not use an array, nor may you perform any I/O operations.

I have used the following code:

```#include "everyNth.h"

uint64_t everyNth(uint64_t Value, uint8_t N) {

uint64_t Accum;
while(Value)
{
Accum = strcat(Value, N));
}

return Accum;
}

```

But am getting errors.

Quote

\$ gcc ./driver.c everyNth.c -std=c99 -o ./driver
everyNth.c: In function `everyNth':
everyNth.c:8: warning: implicit declaration of function `strcat'
everyNth.c:8: warning: passing arg 1 of `strcat' makes pointer from integer without a cast
everyNth.c:8: error: too few arguments to function `strcat'
everyNth.c:8: warning: assignment makes integer from pointer without a cast

Any tips? I'm following the example in the book exactly (or so I thought).

everyNth.h is
```#ifndef EVERYNTH_H
#define EVERYNTH_H
#include <stdint.h>

///////////////////////////////////////////////////////////////////////
//
//  Forms an integer value by taking the Nth digits of Value, starting
//  at the low digit, and concatenating those digits.
//
// Examples:
//     everyNth(12345, 1) = 12345
//     everyNth(12345, 2) =   135
//     everyNth(12345, 3) =    25
//     everyNth(12345, 4) =    15
//     everyNth(12345, 5) =     5
//
// Pre:      Value and N have been initialized.
//           N > 0.
// Post:
// Returns:  integer value formed by concatenating the specified
//           digits of Value; if N = 0, the return value is undefined
//
// Restrictions:
//    You may use any integer operations supported in C.  You may also
//    use any selection (if, if..else, etc) or iteration (for, while)
//    constructs.
//    You may not use an array, nor may you perform any I/O operations.
//
uint64_t everyNth(uint64_t Value, uint8_t N);

#endif

```

and driver.c is as follows:
```#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>

#include "everyNth.h"

void testEveryNth();

int main(int argc, char** argv) {

testEveryNth();

return 0;
}

void testEveryNth() {

uint64_t Value = 12345;
uint8_t  N     =     2;

uint64_t Result = everyNth(0, N);

printf("%21"PRIu64"%3"PRIu8"%21"PRIu64"\n", Value, N, Result);

}

```

This post has been edited by CoryMore: 12 October 2012 - 09:21 PM

Is This A Good Question/Topic? 0

## Replies To: Concatenation of strings in C

### #2 jjl

• Engineer

Reputation: 1120
• Posts: 4,646
• Joined: 09-June 09

## Re: Concatenation of strings in C

Posted 12 October 2012 - 09:24 PM

strcat takes two char pointers, you are passing unsigned integers? You can combined two numbers by using basic arithmetic
```int a = 5;
int b = 6;
int comb = a * 10 + b; // = 56

```

Of course, it takes a little more logic to combined numbers of different size.

This post has been edited by jjl: 12 October 2012 - 09:24 PM

### #3 jimblumberg

Reputation: 4628
• Posts: 14,538
• Joined: 25-December 09

## Re: Concatenation of strings in C

Posted 12 October 2012 - 09:26 PM

The strcat() function only works with C-strings not numbers. Your variables N and Value are numbers. Also to use strcat() you would need to include the string.h header file. But since you're dealing with numbers you can't use strcat() or any of the other string functions without first converting these numbers to C-strings.

Jim

### #4 CoryMore

Reputation: 6
• Posts: 111
• Joined: 26-June 12

## Re: Concatenation of strings in C

Posted 12 October 2012 - 09:49 PM

```#include "everyNth.h"
#include <stdio.h>
#include <string.h>

uint64_t everyNth(uint64_t Value, uint8_t N) {

uint64_t Accum;
char *strValue;
char *strN;

sprintf(strValue, "%d", Value);
sprintf(strN, "%d", N);

while(Value)
{

strcat(strValue, strN);
}

return Accum;
}

```

My output though, doesn't seem to match the example:

Quote

\$ ./driver
12345 2 4294967298

### #5 jjl

• Engineer

Reputation: 1120
• Posts: 4,646
• Joined: 09-June 09

## Re: Concatenation of strings in C

Posted 12 October 2012 - 09:57 PM

You can't just declare a pointer and assume it points to valid memory. When you declare a pointer, it points to meaningless garbage. You need to point that thing to valid memory before manipulating data at it's target.

Ofcourse, this could all be avoided by using characters arrays

```//char *strValue; no need for these
//char *strN;

char strValue[50];
char strN[50];

sprintf(strValue, "%d", Value);
sprintf(strN, "%d", N);

```

And this is an infinite loop if value is any Value is anything but zero

```while(Value) //value never changes
{
strcat(strValue, strN);
}

```

This post has been edited by jjl: 12 October 2012 - 09:59 PM

### #6 CoryMore

Reputation: 6
• Posts: 111
• Joined: 26-June 12

## Re: Concatenation of strings in C

Posted 12 October 2012 - 10:04 PM

I can't use arrays in this particular exercise.

I did change to arrays just to see it, I still got an odd number.

```#include "everyNth.h"
#include <stdio.h>
#include <string.h>

uint64_t everyNth(uint64_t Value, uint8_t N) {

uint64_t Accum;
char strValue[100];
char strN[100];

sprintf(strValue, "%d", Value);
sprintf(strN, "%d", N);

while(N > 0)
{
strcat(strValue, strN);
}

return Accum;
}

```

and got

Quote

\$ ./driver
12345 2 3617008641903833650

### #7 CoryMore

Reputation: 6
• Posts: 111
• Joined: 26-June 12

## Re: Concatenation of strings in C

Posted 12 October 2012 - 10:15 PM

Realized using while wasn't the best idea, changed to if.

```#include "everyNth.h"
#include <stdio.h>
#include <string.h>

uint64_t everyNth(uint64_t Value, uint8_t N) {

uint64_t Accum;
char strValue[100];
char strN[100];

sprintf(strValue, "%d", Value);
sprintf(strN, "%d", N);

if(N > 0)
{
strcat(strValue, strN);
}

return Accum;
}

```

Output still odd

Quote

\$ ./driver
12345 2 6999779920731209496

### #8 jjl

• Engineer

Reputation: 1120
• Posts: 4,646
• Joined: 09-June 09

## Re: Concatenation of strings in C

Posted 12 October 2012 - 10:23 PM

Do you ever assign Accum to anything before returning it?

### #9 jimblumberg

Reputation: 4628
• Posts: 14,538
• Joined: 25-December 09

## Re: Concatenation of strings in C

Posted 13 October 2012 - 05:26 AM

Why are you doing all that string manipulation, you never use the string?

Jim

### #10 CoryMore

Reputation: 6
• Posts: 111
• Joined: 26-June 12

## Re: Concatenation of strings in C

Posted 13 October 2012 - 06:17 AM

I was using string manipulation because that was the only way I really thought to concatenate. Honestly, the instructions seem vague to me, but I assume that's due to my lack of familiarity with C.

Instructions:

Quote

Forms an integer value by taking the Nth digits of Value, starting
at the low digit, and concatenating those digits. *
Examples:
everyNth(12345, 1) = 12345
everyNth(12345, 2) = 135
everyNth(12345, 3) = 25
everyNth(12345, 4) = 15
everyNth(12345, 5) = 5 *

I was thinking about the Accum variable, I have been working on that, and my number is much closer, though still not correct.

```#include "everyNth.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

uint64_t everyNth(uint64_t Value, uint8_t N) {

uint64_t Accum;
char *strValue;
char *strN;

sprintf(strValue, "%d", Value);
sprintf(strN, "%d", N);

if(N > 0)
{
strcat(strValue, strN);
Accum = atoi (strValue);
}

return Accum;
}

```

Results:

Quote

\$ ./driver
12345 2 2

I believe I'm stupposed to end up at 135.

### #11 jimblumberg

Reputation: 4628
• Posts: 14,538
• Joined: 25-December 09

## Re: Concatenation of strings in C

Posted 13 October 2012 - 06:43 AM

You don't want to "add" your second number to your string, you want to select every Nth number starting with the last.

12345, 1 select 1 2 3 4 5
12345, 2 select 1 2 3 4 5
12345, 3 select 1 2 3 4 5
12345, 4 select 1 2 3 4 5
12345, 5 select 1 2 3 4 5

Do you see the pattern???

Jim

This post has been edited by jimblumberg: 13 October 2012 - 07:49 AM

### #12 CoryMore

Reputation: 6
• Posts: 111
• Joined: 26-June 12

## Re: Concatenation of strings in C

Posted 13 October 2012 - 06:58 AM

Oh, well yes, now that you put it that way, I do. I was focused on the concatenating, but I suppose that just means they want them next to each other. Back to the drawing board then. Thank you, sorry that was so obvious and I missed it.

This post has been edited by CoryMore: 13 October 2012 - 06:59 AM

### #13 baavgai

• Dreaming Coder

Reputation: 6293
• Posts: 13,459
• Joined: 16-October 07

## Re: Concatenation of strings in C

Posted 13 October 2012 - 07:35 AM

The best advice I could give you for this is don't use strings at all. Use % / and recursion.

### #14 CoryMore

Reputation: 6
• Posts: 111
• Joined: 26-June 12

## Re: Concatenation of strings in C

Posted 13 October 2012 - 10:33 PM

Is there an equivalent of str.at for C? That would make what I'm doing much easier...

### #15 jjl

• Engineer

Reputation: 1120
• Posts: 4,646
• Joined: 09-June 09

## Re: Concatenation of strings in C

Posted 13 October 2012 - 10:41 PM

str.at? are you referring to an equivalent of std::string at() for C strings? Well their is not dedicated indexing function for C strings because you can index the memory directly using the [] operator, or pointer arithmetic.

```char str[] = "abc";
str[0] = "b";
printf("%s", str); //prints bbc

```