6 Replies - 454 Views - Last Post: 03 October 2015 - 09:18 AM Rate Topic: -----

#1 shadow125  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 21-April 15

[C] String is not being printed correctly

Posted 02 October 2015 - 02:00 PM

Hey guys, I have this program and I noticed something odd. The program below removes part of a string and prints the result. Here is the code: (I wrote this program a while ago when I were less knowledgeable, so sorry if the code is not optimized)

// Program to remove a string

#include <stdio.h>
#include <string.h>
int main (void)
{
    char removeString (char text[], int source, int characters, char result[]);   
    
    char result[111];
    removeString("My cpu is not AMD", 9, 4, result);    /* 4 is the number of characters to be removed, and 9 is position to be removed. The result will be copied to result[] */
 
return 0;
}

char removeString (char text[], int source, int characters, char result[])
{
    int i, lentgh = 0;

    lentgh = strlen(text);

    
    for (i = 0; i != source; ++i)
        result[i] = text[i];

    for (i = (source + characters); text[i] != '\0'; ++i)
        result[i] = text[i];    
 
    for (i = 0; i != lentgh; ++i)    // when using this for loop it displays the string as it should
        printf("%c", result[i]);
    printf("\n");

    printf("%s\n\n", result);          // but when I use this printf it shows only half the string
}    




Output:

My cpu is AMD

My cpu is




However, when I use the for loop to printf the result[] variable, everything is ok, it prints the result correctly, but if I use only a printf it displays only half the string. Why is this happening? Is there anything to do with the null terminator? It seems to be something really simple, but I am trying to figure it out for almost an hour. Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: [C] String is not being printed correctly

#2 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: [C] String is not being printed correctly

Posted 02 October 2015 - 02:33 PM

    for (i = 0; i != source; ++i)
        result[i] = text[i];

    for (i = (source + characters); text[i] != '\0'; ++i)
        result[i] = text[i];

You are skipping several character positions (4 or 5?) when populating result. You need to continue from where the previous i left off.

The skipped positions are, I assume, non-printable characters (someone may correct me), which is why the code as a whole seems to work.

This post has been edited by andrewsw: 02 October 2015 - 02:34 PM

Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5207
  • View blog
  • Posts: 13,953
  • Joined: 18-April 07

Re: [C] String is not being printed correctly

Posted 02 October 2015 - 02:36 PM

Well first of all your logic here is just not going to work. You can't do a simple loop from (source + characters) because if you had a string that was say 5 characters long and you specified a source that was 4 and characters that was 8 it would put you into a section of memory past the end of your string and cause all kinds of problems.

So what I suggest you do first is make sure that source and characters are going to result in a valid range for your string. If they are not valid, don't even bother continuing.

As for what you have here, I don't see your for loop printing correctly either. Neither way is printing right. This is partly because you are also not taking into account the null terminator character correctly. Where, in your code, does a null terminator ever get added to result?

:)
Was This Post Helpful? 0
  • +
  • -

#4 shadow125  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 21-April 15

Re: [C] String is not being printed correctly

Posted 02 October 2015 - 03:22 PM

View Postandrewsw, on 02 October 2015 - 02:33 PM, said:

    for (i = 0; i != source; ++i)
        result[i] = text[i];

    for (i = (source + characters); text[i] != '\0'; ++i)
        result[i] = text[i];

You are skipping several character positions (4 or 5?) when populating result. You need to continue from where the previous i left off.

The skipped positions are, I assume, non-printable characters (someone may correct me), which is why the code as a whole seems to work.


I skipped these positions so I could remove a part of the string, the first for loop copies the first part of the string to result[] "My cpu is", and the next for loop copies "amd" so you omit the " not".
Was This Post Helpful? 0
  • +
  • -

#5 shadow125  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 21-April 15

Re: [C] String is not being printed correctly

Posted 02 October 2015 - 03:31 PM

View PostMartyr2, on 02 October 2015 - 02:36 PM, said:

Well first of all your logic here is just not going to work. You can't do a simple loop from (source + characters) because if you had a string that was say 5 characters long and you specified a source that was 4 and characters that was 8 it would put you into a section of memory past the end of your string and cause all kinds of problems.

So what I suggest you do first is make sure that source and characters are going to result in a valid range for your string. If they are not valid, don't even bother continuing.

As for what you have here, I don't see your for loop printing correctly either. Neither way is printing right. This is partly because you are also not taking into account the null terminator character correctly. Where, in your code, does a null terminator ever get added to result?

:)/>/>/>


Sorry, I guess I didn't explain my logic very clearly. On my first call to the function removeString I have two parameters which are "source = 9" and "characters = 4". Source is the position in the array to start removing the characters, in this case after the letter "s", and "characters" is the number of characters to remove starting from source, thus the function should omit anything from text[9] to text[9 + 4]. By the way, I didn't insert any null characters, how can I do it?

This post has been edited by shadow125: 02 October 2015 - 03:34 PM

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: [C] String is not being printed correctly

Posted 02 October 2015 - 03:39 PM

Quote

I skipped these positions so I could remove a part of the string, the first for loop copies the first part of the string to result[] "My cpu is", and the next for loop copies "amd" so you omit the " not".

I know what you are trying to do, but your code doesn't append "amd" immediately after "My cpu is".

But the points raised by Martyr2 are more important.
Was This Post Helpful? 0
  • +
  • -

#7 shadow125  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 21-April 15

Re: [C] String is not being printed correctly

Posted 03 October 2015 - 09:18 AM

View Postandrewsw, on 02 October 2015 - 03:39 PM, said:

Quote

I skipped these positions so I could remove a part of the string, the first for loop copies the first part of the string to result[] "My cpu is", and the next for loop copies "amd" so you omit the " not".

I know what you are trying to do, but your code doesn't append "amd" immediately after "My cpu is".

But the points raised by Martyr2 are more important.


I solved my problem by making use of the strcpy and strcat functions.

#include <stdio.h>
#include <string.h>

int main (void)
{
    char removeString (char text[], int source, int characters, char result[]);
    
    char result[111];
    removeString("My cpu is not AMD", 9, 4, result);
 
return 0;
}

char removeString (char text[], int source, int characters, char result[])
{
    int i;
    char aux[111];

    for (i = 0; i != source; ++i)
        result[i] = text[i];

    strcpy(aux, result);    // copies the first part to an auxiliary array
    strcpy(result, text + (source + characters));   // copies second part to result
    strcat(aux, result);    // concatenates both parts

    printf("%s\n", aux);
return 0;
}    
 


Apparently the strcat adds a null character after merging two strings, thus eliminating my problem. It seems that whenever I learn something new there is always an easier way to do it. Thanks all of you for the help.

This post has been edited by shadow125: 03 October 2015 - 09:20 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1