473,382 Members | 1,745 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,382 software developers and data experts.

Print all suffix of String in C

thatos
105 100+
I have a string S and I want to print all its suffix, I tried the following but I seem to be missing a number of letters;
Expand|Select|Wrap|Line Numbers
  1. char *S;
  2. scanf("%s", &S);
  3. int l,i;
  4. l = strlen(S);
  5. for(i = 0;i < l;i++){
  6.   printf("%s",&S[i]);
  7. }
  8.  
When I input a word of length greater the 8, I lose all the letters after the 8th char.
for example
Expand|Select|Wrap|Line Numbers
  1. mississippi //this is the input
  2. mississippi
  3. ississi
  4. ssissi
  5. sissi
  6. issi
  7. ssi
  8. si
  9. i
  10.  
  11.  
  12.  
  13.  
Jul 13 '11 #1

✓ answered by Banfa

Expand|Select|Wrap|Line Numbers
  1. char *S;
  2. scanf("%s", &S);
  3.  
This is undefined behaviour, you do not allocate any memory to S and then rather than passing where S points to you pass a pointer to where S is to scanf.

Reading a string should look like this using scanf

Expand|Select|Wrap|Line Numbers
  1. char S[100];
  2. scanf("%s", S);
  3.  
But that is insecure as it allows buffer overflow so you should rather do this

Expand|Select|Wrap|Line Numbers
  1. char S[100];
  2. fgets(S, sizeof S, stdin);
  3.  

4 7655
whodgson
542 512MB
I don`t think you need the for loop (l5) or the[i] (l6)
The loop is for-shortening the word.
Jul 14 '11 #2
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. char *S;
  2. scanf("%s", &S);
  3.  
This is undefined behaviour, you do not allocate any memory to S and then rather than passing where S points to you pass a pointer to where S is to scanf.

Reading a string should look like this using scanf

Expand|Select|Wrap|Line Numbers
  1. char S[100];
  2. scanf("%s", S);
  3.  
But that is insecure as it allows buffer overflow so you should rather do this

Expand|Select|Wrap|Line Numbers
  1. char S[100];
  2. fgets(S, sizeof S, stdin);
  3.  
Jul 14 '11 #3
thatos
105 100+
Thanks but what if I don't know the maximum length of the string?
Jul 15 '11 #4
Banfa
9,065 Expert Mod 8TB
What if you don't know the maximum length of the string being input?

You guess or you specify the maximum your program excepts you do something more intelligent reading a few characters at a time and transfering them to a dynamically allocated buffer (malloc'd) that you can resize if required.
Jul 18 '11 #5

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

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
4
by: Locusta | last post by:
Hello, I have been struggeling for replacing a string in a string. The snippet from the program below replaces the <, & and > with the XML equivalent values. In the program, I allocate space...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
4
by: Emilio | last post by:
Question about Shared Sub Connect(server As , message As ) Why is in square brackets? Is it like Shared Sub Connect(server() As String, message() As String)
2
by: Badass Scotsman | last post by:
Hello, Using VB and ASP,NET I would like to be able to search a STRING for a smaller STRING within, based on the characters which appear before and after. For example: String1 = " That was...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.