LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-15-2015, 11:23 AM   #1
Ananda Bbau
LQ Newbie
 
Registered: May 2012
Posts: 9

Rep: Reputation: Disabled
Doubt in program


Hi All,

I have written a small program to avoid "%" symbol from the given string. But while i printing getting the statements before to % symbol.

#include<iostream>
using namespace std;

int main()
{
string s="a%b";

char str[1024],str1[1024];

int i=0,j=0,k=0;

strncpy(str,s.c_str(),sizeof(str));
//str[sizeof(str)-1]=0;

while(str[i]!='\0')
{
if(str[i]!='%')
{
str1[j]=str[i];
i++;
}
else
{
i++;
}
j++;

}

string str2(str1);
cout<<"The string is:"<<str2<<endl;
return 0;
}

ouput:
cds-build4:502> g++ -g make1.cpp -o make
cds-build4:503> ./make
The string is:a

Expected output:
The string is:ab

Please help me to solve it and let me know where did i mistake.

Thanks,
G.Ananda babu.
 
Old 07-15-2015, 11:58 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,242

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
I didn't feel like looking through your program, but I did it like this:

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
	const char *s = "ab";
	const size_t s_len = strlen(s);
	char *s2 = calloc(s_len, sizeof(char));
	int i = 0;
	int offset = 0;

	for (i = 0; i < s_len + 1; i++)
	{
		if (s[i] == '%')
		{
			offset += 1;
		}
		s2[i] = s[i + offset];
	}

	printf("%s\n", s2);
	free(s2);
	s2 = 0;

	return 0;
}

Last edited by dugan; 07-15-2015 at 04:15 PM.
 
Old 07-15-2015, 12:16 PM   #3
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
I am by no means an expert on C programming. But I don't really see the need for strncpy, and your logic seems a bit off. Also, PLEASE use code tags. It makes things so much easier to read.

Anyway, with that out of the way, here's how I would have done:
Code:
#include <stdio.h>
#include <string.h>

int main()
{
    
    char teststring[] = "abc%defg%hi";
    char newstring[100] = "";
    int i = 0;
    int j = 0;
    
    while (teststring[i] != '\0') {
        if (teststring[i] != '%') {
            newstring[j] = teststring[i];
            j++;
        }
        i++;
    }
    
    printf("%s\n", newstring);
        
    return 0;
}
My output:
Code:
./charsearch 
abcdefghi
Best regards,
HMW

Edit: I see that dugan beat me to it. His/her code is probably much better due to the more efficient memory handling (calloc).

Last edited by HMW; 07-15-2015 at 12:19 PM.
 
1 members found this post helpful.
Old 07-15-2015, 09:21 PM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Indentation will help. You're incrementing "j" in the wrong place.

You must also be sure to null-terminate your finished string.

Wouldn't you rather be doing this in C++ or in some other language?

Last edited by sundialsvcs; 07-15-2015 at 09:22 PM.
 
Old 07-16-2015, 12:07 AM   #5
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
assuming s contains the input string, s1 is going to have the output string and i is an int variable, then the main loop would be:
Code:
strcpy(s1,"") ;
for (i=0;i<strlen(s);i++) {
 if s[i] != '' strcat(s1,s[i]) ;
}
OK

Last edited by AnanthaP; 07-16-2015 at 12:19 AM.
 
Old 07-16-2015, 12:49 AM   #6
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
find the first occurrence,
memmove everything behind sizeof (occurrence) back
as long as find returns something
 
Old 07-16-2015, 02:16 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
HMW's solution is almost perfect, the only thing it lacks is the terminating zero:
Code:
    }
    newstring[j] = '\0';
 
1 members found this post helpful.
Old 07-16-2015, 07:09 AM   #8
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by NevemTeve View Post
HMW's solution is almost perfect, the only thing it lacks is the terminating zero:
Code:
    }
    newstring[j] = '\0';
Thanks first and foremost for the kind words. And secondly for pointing out that i forgot to add the null character after the loop!

Last edited by HMW; 07-16-2015 at 07:13 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Socket program doubt Gwapala Linux - Networking 1 07-22-2010 02:37 PM
Doubt in a simple basic C program.... thefountainhead100 Programming 9 03-15-2008 01:01 AM
ipv6 program doubt ayeshaseerin Linux - Networking 0 05-09-2006 07:09 AM
Linux Newbie: doubt regarding program files amit_chandak Linux - Newbie 5 03-04-2006 03:57 PM
Program to eject cdrom drive ... doubt tuxfood Programming 3 07-20-2005 03:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration