I am playing with pointers in the K&R book and I wrote this program that swaps integers and measures the length of a string with a pointer. The first part works but my string length function does nothing. The program compiles and runs the first part and then the program stops responding.
#include <stdio.h>
extern int a2 = 4;
extern int b2 = 5;
void swap(int *px, int *py);
int strlen2(char *s);
//int printLabel(char *thelabel, char newliner);
//int printLabel(char *thelabel, char newliner)
//{
// int stringlength1=(strlen2(thelabel));
// return stringlength1;
//}
void swap(int *px, int *py) /* interchange *px and *py */
{
int temp;
temp = *px;
*px = *py;
*py = temp;
}
int strlen2(char *s)
{
int n;
for (n = 0; *s != '\0', s++;)
n++;
return n;
}
int main()
{
int a=4;
int b=5;
char newliner = '\n';
swap(&a,&b);
swap(&a2,&b2);
printf("%d",a);
printf("%c",newliner);
printf("%d",b);
printf("%c",newliner);
printf("%d",a2);
printf("%c",newliner);
printf("%d",b2);
printf("%c",newliner);
char sumstring[]="boo";
char *labelPtr;
labelPtr = sumstring;
int length = strlen2(labelPtr);
printf("%d",length);
return 0;
}
The problem is that this:
for (n = 0; *s != '\0', s++;)
is a semi-infinite loop. It checks for the terminating NUL, but then it ignores the result of that comparison and increements s, continuing the loop if it is non-null. Once it gets past the end of the string, the result is undefined behavior, but its likely to either loop forever or crash.
You probably meant
for (n = 0; *s != '\0'; s++)
In the for loop, the second expression is usually a condition, in your case *s != '\0'.
The 3rd expression is the increment, where you are supposed to increment the s pointer.
This is working fine:
int strlen2(char *s)
{
int n;
for (n = 0; *s != '\0'; s++)
n++;
return n;
}
Replace your code with the following:
int strlen2(char *s)
{
int n = 0;
while(s[n] != '\0')
++n;
return n;
}
looks like a typo in the code. shouldn't this line be:
for (n = 0; *s != '\0'; s++)
instead of
for (n = 0; *s != '\0', s++;)
In this for statmenet
for (n = 0; *s != '\0', s++;)
in the condition part there is used the comma operator
*s != '\0', s++
Its results is the value of the last subexpression that is of s++. As pointer s is not equal to 0 then you get at least very long sycle.
I think you meant instead
for (n = 0; *s != '\0'; s++ )
Related
As the title says. I don't Understand why this code gives me segfault!
#include <stdio.h>
void cp(char s[], char d[]);
int main () {
char s[100] = "hi there how are you";
char d[100];
cp(s, d);
printf("%s\n++++++++\n%s\n", s, d);
return 0;
}
void cp(char s[], char d[]) {
int i, p = 0;
while (s[i] != '\0') {
d[i] = s[i];
++i;
++p;
}
}
I know the cp implementation is terrible! I wrote it like this just for fun, then the segfault happened.
In this line of code:
int i, p = 0;
you only initialize p, variable i is uninitialized, reading from it leads to UB.
Proper loop could be written like this:
for( size_t i = 0; ( d[i] = s[i] ) != 0; ++i );
(it will also copy null-terminator which you would be missing if initialize i properly)
Another variant is classical C way:
void cp(const char *s, char *d)
{
while( *d++ = *s++ );
}
but usually in C target is the first parameter, not the second (for example strcpy())
You copy only while:
while (s[i] != '\0')
so '\0' isn't copied so when you run:
printf("%s\n++++++++\n%s\n", s, d);
you get a segfault.
Also i is uninitialised:
int i, p = 0;
using separate lines avoids this typo:
int i = 0;
int p = 0;
an uninitialised i can blow up:
d[i] = s[i];
causing a segfault.
I made code which will for string "aabbcc" return "abc" but in cases when there is more letters like "aaa" it will return "aa" instead of just one.
Here is the code I made.
void Ponavljanje(char *s, char *p) {
int i, j = 0, k = 0, br = 0, m = 0;
for (i = 0; i < strlen(s) - 1; i++) {
for (j = i + 1; j < strlen(s); j++) {
if (s[i] == s[j]) {
br++;
if (br == 1) {
p[k++] = s[i];
}
}
}
br = 0;
}
p[k] = '\0';
puts(p);
}
For "112233" output should be "123" or for "11122333" it should be also "123".
Avoid repeated calls to strlen(s). A weak compiler may not see that s is unchanged and call strlen(s) many times, each call insuring a cost of n operations - quite inefficient. #arkku.1 Instead simply stop iterating when the null character detected.
Initialize a boolean list of flags for all char to false. When a character occurs, set the flag to prevent subsequent usage. Be careful when indexing that list as char can be negative.
Using a const char *s allows for wider allocation and helps a compiler optimization.
Example:
#include <stdbool.h>
#include <limits.h>
void Ponavljanje(const char *s, char *p) {
const char *p_original = p;
bool occurred[CHAR_MAX - CHAR_MIN + 1] = { 0 }; // all values set to 0 (false)
while (*s) {
if (!occurred[*s - CHAR_MIN]) {
occurred[*s - CHAR_MIN] = true;
*p++ = *s;
}
s++;
}
*p = '\0';
puts(p_original);
}
1 #wrongway4you comments that many compilers may assume the string did not change and optimize out the repeated strlen() call. A compliant compiler cannot do that though without restrict unless it is known that in all calls, s and p do not overlap. A compiler otherwise needs to assume p may affect s and warrant a repeated strlen() call.
does the work with a complexity O(n)
I suppose programming can give rmg
void Ponavljanje(char *s,char *p)
{
char n[256] = {0};
int i = 0;
while (*s) {
switch (n[(unsigned char) *s]) {
case 0:
n[(unsigned char) *s] = 1;
break;
case 1:
p[i++] = *s;
n[(unsigned char) *s] = 2;
}
s += 1;
}
p[i] = 0;
puts(p);
}
While the inner loop checks br to only copy the output on the first repetition, the outer loop still passes over each repetition in s on future iterations. Hence each further occurrence of the same character will run a separate inner loop after br has already been reset.
With aaa as the input, both the first and the second a cause the inner loop to find a repetition, giving you aa. In fact, you always get one occurrence fewer of each character in the output than there is in the input, which means it only works for 1 or 2 occurrences in the input (resulting in 0 and 1 occurrences, respectively, in the output).
If you only want to remove the successive double letters, then this function would be sufficient, and the examples given in the question would fit:
#include <stdio.h>
void Ponavljanje(char *s,char *p)
{
char dd = '\0';
char *r;
if(s == NULL || p == NULL)
return;
r = p;
while(*s){
if(*s != dd){
*r = *s;
dd = *s;
r++;
}
s++;
}
*r = '\0';
puts(p);
}
int main(void)
{
char s[20] = "1111332222";
char p[20];
Ponavljanje(s,p);
}
Here is something that works regardless of order:
#include <stdio.h>
#include <string.h>
void
repeat(char *s, char *p)
{
int slen;
int sidx;
int pidx;
int plen;
int schr;
slen = strlen(s);
plen = 0;
for (sidx = 0; sidx < slen; ++sidx) {
schr = s[sidx];
// look for duplicate char
int dupflg = 0;
for (pidx = 0; pidx < plen; ++pidx) {
if (p[pidx] == schr) {
dupflg = 1;
break;
}
}
// skip duplicate chars
if (dupflg)
continue;
p[plen++] = schr;
}
p[plen] = 0;
puts(p);
}
int
main(void)
{
char p[100];
repeat("112233",p);
repeat("123123",p);
return 0;
}
Note: As others have mentioned, strlen should not be placed in the loop condition clause of the for [because the length of s is invariant]. Save strlen(s) to a separate variable and loop to that limit
Here is a different/faster version that uses a histogram so that only a single loop is required:
#include <stdio.h>
#include <string.h>
void
repeat(char *s, char *p)
{
char dups[256] = { 0 };
int slen;
int sidx;
int pidx;
int plen;
int schr;
slen = strlen(s);
sidx = 0;
plen = 0;
for (sidx = 0; sidx < slen; ++sidx) {
schr = s[sidx] & 0xFF;
// look for duplicate char
if (dups[schr])
continue;
dups[schr] = 1;
p[plen++] = schr;
}
p[plen] = 0;
puts(p);
}
int
main(void)
{
char p[100];
repeat("112233",p);
repeat("123123",p);
return 0;
}
UPDATE #2:
I would suggest iterating until the terminating NUL byte
Okay, here's a full pointer version that is as fast as I know how to make it:
#include <stdio.h>
#include <string.h>
void
repeat(char *s, char *p)
{
char dups[256] = { 0 };
char *pp;
int schr;
pp = p;
for (schr = *s++; schr != 0; schr = *s++) {
schr &= 0xFF;
// look for duplicate char
if (dups[schr])
continue;
dups[schr] = 1;
*pp++ = schr;
}
*pp = 0;
puts(p);
}
int
main(void)
{
char p[100];
repeat("112233",p);
repeat("123123",p);
return 0;
}
I tried following two ways to reverse a string in C using char pointers:
Option 1:
void stringreverse(char *s){
int n = stringlength(s) - 2;
while(*s != '\0'){
char c = *s;
*s = *(s+n);
*(s+n) = c;
s++;n--;
}
}
Option 2:
void stringreverse(char *s){
char *p = s + stringlength(s) - 2;
while(*s != '\0'){
char c = *s;
*s = *p;
*p = c;
s++,p--;
}
}
None of the above works. Hints on why?
The problem is that your code reverses the string and then reverse it again, because your loop goes from 0 to len (when *s==\0), it should stop at (len-1)/2
You should try this :
void stringreverse(char* s){
int len = strlen(s)-1;
int i;
for(i=0;i<len/2;i++){
char tmp = s[i];
s[i] = s[len-i];
s[len-i]=tmp;
}
}
To reverse the string you should swap the chars between the beginning and the end of the string until they meet in the middle, the way you did will reverse it and then reverse it again to the original string. Also there is strlen in standard C. Anyway using your definition of stringlength, it should be:
void stringreverse(char *s){
int n = stringlength(s) - 2;
int i;
while(i = 0; i < n / 2; i++) {
char c = s[i];
s[i] = s[n-i];
s[n-i] = c;
}
}
complete working sample using pointers:
#include <stdio.h>
void reverse(char *p){
char c;
char* q = p;
while (*q) q++;
q--; // point to the end
while (p < q){
c = *p;
*p++ = *q;
*q-- = c;
}
}
int main(){
char s[] = "DCBA";
reverse(s);
printf("%s\n", s); // ABCD
}
p: points to start of string.
q: points to the end of string.
then swap their contents.
simple and easy.
I wrote the following function to inverse a string s
char *strinverse( const char *s ){
char *t;
int i = 0;
while (*s) {
s++;
i++;
}
while (i >= 0){
s--;
*t = *s;
t++;
i--;
}
*t = '\0';
return t;
}
int main(void){
char v[4]="abc";
char r[4];
char *pr = r;
pr = strinverse(v);
printf("%s", pr);
return 0;
}
The idea is to find out the length of the string s in the first while-loop, then to decrease the pointer of s while copying the respective values into t. For some reason the program crashes and the compiler gives me no information. Maybe there's something wrong in the main function? Thanks for your advices!
Answer edited
#include<stdio.h>
#include<stdlib.h>
char *strinverse(const char *s ){
char *t, *p;
int i = 0;
while (*s) {
s++;
i++;
}
t = (char*)malloc((i + 1) * sizeof(char)); //added this!
p = t;
while (i >= 0){
s--;
*t = *s;
t++;
i--;
}
*t = '\0';
return p;
}
int main(void){
char v[4]="abc";
char *pr;
pr = strinverse(v);
printf("%s\n", pr);
return 0;
}
The reason that program crashes is that you have not allocated space for pointer t. In this case your program invokes undefined behavior. Allocate space for t
t = malloc(i + 1);
Do not forget to free memory at the end using free(t).
I would use a function that changes the string in-place instead.
void reversestr(char *s)
{
char tmp;
size_t i, len = strlen(s);
for (i = 0; i < len / 2; i++) {
tmp = s[i];
s[i] = s[len - 1 - i];
s[len - 1 - i] = tmp;
}
s[len] = '\0';
}
If you need the reversed string separately, you can just use strdup before you call reversestr. BTW: function names that start with "str" are reserved for functions of the C standard library.
you have not make malloc in the pointer "t" and you have problem in this line "*t = *s;"
I'm trying to reverse a string in C. The reverse function simply assigns the character at a given location (in a for loop) to a temp object. I cannot see any logic errors within the program, and the program compile successfully under gcc 4.7.2 with this command:
gcc -Wall -std=c99 reverse.c
To recreate the problem:
1.) Run the program and enter a string into your shell
2.) Once finished inputting, press enter/and or your EOF signal.
The problem is that neither the original string is printed, or the reversed string. This is also an exercise from K&R second edition, if you have completed this exercise, a different solution to mine would be appreciated.
I think the bug is caused by the absence of a null character, the famous printf requires a null terminated string to print input to cin. The getline function assigns a null character to the end of the array, surely the null character will be the first character in the string thereto ending the printf (and thus no character/literal is printed).
#include <stdio.h>
#define MAXLINE 1000
int geline(char s[], int lim);
void reverse(char line[], int length);
int main()
{
char s[MAXLINE];
char t[MAXLINE];
int k, len;
while ((len = getline(s, MAXLINE)) > 0) {
if (len > 1)
reverse(s, len);
}
printf("%s", s);
return 0;
}
void reverse (char input[], int length)
{
char temp[MAXLINE];
int j = length;
for (int i = 0; i < length; ++i, --j) {
temp[i] = input[i];
input[i] = input[j];
input[j] = temp;
}
}
int getline(char s[], int lim)
{
int c, i;
for (i=0; (c=getchar()) != EOF && c!='\n'; ++i)
s[i] = c;
if (c== '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
There are two logic errors:
int j = length; should be int j = length - 1;
temp[i] = input[i] ... input[j] = temp;
There are two approaches for that last error:
Define temp as a single char: char temp; ... temp = input[i]; input[i] = input[j]; input[j] = temp;
Use the correct index in temp: temp[i] = input[i]; input[i] = input[j]; input[j] = temp[i]
Try this code:
#include <stdio.h>
#define MAXLINE 1000
int geline(char s[], int lim);
void reverse(char line[], int length);
int main () {
char s[MAXLINE];
char t[MAXLINE];
int k, len;
while ((len = getline(s, MAXLINE)) > 0) {
if (len > 1)
reverse(s, len);
}
printf("%s", s);
return 0;
}
void reverse (char input[], int length) {
char temp;
int j = length - 1;
for (int i = 0; i < j; ++i, --j) {
temp = input[i];
input[i] = input[j];
input[j] = temp;
}
}
int getline (char s[], int lim) {
int c, i;
for (i=0; (c=getchar()) != EOF && c!='\n'; ++i)
s[i] = c;
if (c== '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
(I did my compiling with -Wall -std=c99 -O3 -g, the -g to allow use of gdb)
Here are the things I noticed and some ways of addressing them. I've tried to hew pretty closely to the style you started with (I would have converted the array decls in the prototypes to pointers, for example, but that's not necessary).
Your getline prototype was missing the t.
int getline(char s[], int lim);
In main, you don't actually need k, t[MAXLINE], and your printf should probably be in the loop so you'll see each word as it's reversed. Note that printf picks up a \n, since the getline below converts both newline and EOF-terminated lines to the same thing (without newlines):
int main()
{
char s[MAXLINE];
int len;
while ((len = getline(s, MAXLINE)) > 0) {
if (len > 0)
reverse(s, len);
printf("%s\n", s);
}
return 0;
}
In above, the getline(s, MAXLINE) could have been getline(s, sizeof(s) / sizeof(*s) - 1) although again, be careful of fencepost errors (note the - 1).
The reverse function can be greatly improved without going over to the madness of xor to skip having a variable (although Daffra's example is interesting, especially in that it correctly stops in the middle). Instead, having the sense to just index up to the halfway point is a clear win. Between that and dropping reducing the temp array to just a temporary character, your general style is retained.
void reverse (char input[], int length)
{
int max = length - 1; /* keep the final NUL in place */
for (int i = 0; i <= max / 2; ++i) {
char ch = input[i];
input[i] = input[max - i];
input[max - i] = ch;
}
}
In the above gcc -O3 can do a serious workover on the code, so there's no real reason to worry that long division is going to be performed on every loop test, etc. For example, gdb reports that i itself gets optimized out automatically, which is pretty interesting. Write good, readable code first, have some faith in your compiler, optimize later.
And last, getline benefits from testing against lim (CRITICAL!) and and converting newlines into NULs.
int getline(char s[], int lim)
{
int i, c;
for (i=0; (i <= lim) && ((c=getchar()) != EOF) && (c != '\n'); ++i)
s[i] = c;
s[i] = '\0';
return i; /* return the index to the final NUL, same as length w/o it */
}
Setting MAXLINE to 10 temporarily shows that this version handles overlong lines fairly gracefully, splitting them into two separate ones without losing any of the characters.
Be careful with strings to very clearly decide whether you want to describe them in terms of length, or in terms of the index to the NUL at the end. This affects how you phrase your loops, limits, variable names, etc, and obviously confusing them is a classic source of fencepost errors.
Hope this helps.
int j = length - 1; // Thanks to #chux
for (int i = 0; i < j; ++i, --j) { // or <= length / 2
char temp = input[i];
input[i] = input[j];
input[j] = temp;
temp is not needed, and not entirely correctly used.
You are twice swapping the values, which restores the swap on the second half of the cycling. :)
Your prototype misses a 't' (geline). Hence maybe
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
is taken?
you can use this fast function :
inline char * reverse(char *p)
{
char *save=p;
char *q = p;
while(q && *q) ++q;
for(--q; p < q; ++p, --q)
*p = *p ^ *q,
*q = *p ^ *q,
*p = *p ^ *q;
return save ;
}
Please have a look at this code:
#include <stdio.h>
#define MAXLINE 1000
int geline(char s[], int lim);
void reverse(char line[], int length);
int main()
{
char s[MAXLINE];
int len;
while ((len = geline(s, MAXLINE)) > 1) {
if (len > 1) {
reverse(s, len);
printf("%s", s);
}
}
return 0;
}
void reverse (char input[], int length)
{
char temp;
int j = length-1;
for (int i = 0; i < j; ++i, --j) {
temp = input[i];
input[i] = input[j];
input[j] = temp;
}
}
int geline(char s[], int lim)
{
int c, i;
for (i=0; (c=getchar()) != EOF && c!='\n'; ++i)
s[i] = c;
if (c== '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
Only 2 changes needed here, and it will do the reverse fine.
Inside function reverse just do this
int j = --length;
Instead of this:
input[j] = temp; //you should use
input[j] = temp[i];