473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

changing from array to pointer format?

Ive tried changing it to pointer format but when i do, the new string without spaces doesnt appear. i appreciate the help!


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. int stripspaces(char str[]);
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     char ar[50];
  8.     cout << "enter a sentence: " <<endl;
  9.     cin.getline(ar,49);
  10.     cout<<" your sentence has " <<stripspaces(ar)<<" spaces "<<endl;
  11.  
  12.     return 0;
  13. }
  14.  
  15. int stripspaces (char str[])
  16. {
  17.     int spacecounter = 0;
  18.     char * p =str;
  19.  
  20.     while (*p)
  21.     {
  22.         if (*p == ' ')
  23.             spacecounter++;
  24.         p++;
  25.     }
  26.     int i =0;
  27.     char temp[50];
  28.     int count= 0;
  29.  
  30.     while (str[i])
  31.     {
  32.         if(str[i]!= ' ')
  33.         {
  34.             temp[count] = str[i];
  35.             count++;
  36.         }
  37.         i++;
  38.     }
  39.     temp[count] = '\0';
  40.     cout <<" The new string without spaces is:" <<temp<<endl;
  41.     return spacecounter;
  42. }
Mar 27 '10 #1
6 1776
newb16
687 512MB
What doesn't work in the program above and what is the expected result?
Mar 27 '10 #2
the program above works but its written in array notation but i have to change it to pointer notation and so i tried writing something like char * t= temp; after line 27 so that later i could refer to the values in temp as *(t +i) and such so that i could change to pointer notation. but when i did that the program doesnt output the new string without the spaces(which is what is supposed to output). it does output the second part correctly, which is the part that tells you how many spaces are in the entered string.
Mar 28 '10 #3
whodgson
542 512MB
Apart from not understanding how your code keeps the screen open I would have thought that line 7 should be char* ar[50]; etc
Mar 28 '10 #4
newb16
687 512MB
I tried writing char *t=temp and replacing temp[count] with *(temp+count) after line 27 and it worked for me - it counts spaces and outputs trimmed string.
Mar 28 '10 #5
hm.. I guess ill try again then! Thank you! would that be sufficient for the program to be considered in pointer notation?
Mar 28 '10 #6
whodgson
542 512MB
Yes.....because thats what it would be using.
Mar 30 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Michael Sgier | last post by:
Hello I want to replace a windows bitmap load function through an equivalent SDL function. I should extract the img height etc. and assign it to the terrainTex array. I've also three beginner...
67
by: Ike Naar | last post by:
Hi, Asking your advice on the following subject: Suppose I want to find out whether a given pointer (say, p) of type *T points to an element of a given array (say, a) of type T. A way to...
10
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
7
by: Jeff K | last post by:
Can you pass an int array by reference to a function and modify selective elements? Here is my code: #include <stdio.h> #define COLUMNSIZE 30 #define ASIZE 5...
1
by: Firewalker | last post by:
I am attempting to change the backColor property on the previously instantiated buttons FROM a listbox_doubleClick event. I thought it would be something like this: If...
30
by: James Daughtry | last post by:
char array; scanf("%19s", &array); I know this is wrong because it's a type mismatch, where scanf expects a pointer to char and gets a pointer to an array of 20 char. I know that question 6.12...
15
by: ankitjain.bvcoe | last post by:
Hi , i don't know why the below mentioned program is giving wrong o/p. #include <stdio.h> #include <stdlib.h> #include <string.h> ARRAY_SIZE 10 int func(void *x) {
29
by: yourmycaffiene | last post by:
Okay, this if my first post so go easy on me plus I've only been using C for a couple of weeks. I'm working on a program currently that requires me to read data from a .dat file into a 2d array and...
8
by: SP | last post by:
The following code crashes after I add the two nested FOR loops at the end, I am starting to learn about pointers and would like to understand what I'm doing wrong. I think the problem is the way...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.